Java 仅仅更改变量的值就可以更改链接列表中节点的值?这不应该发生

Java 仅仅更改变量的值就可以更改链接列表中节点的值?这不应该发生,java,linked-list,Java,Linked List,所以,我正在用Java制作一个国际象棋引擎。给定当前的电路板配置,AI应该找出它可以做出的每个可能的动作,将其添加到所有可能动作的链接列表中,然后返回该列表。目前,我正在测试以下电路板配置: /*bRbNbBbQbKbBbNbR bPbPbPbPbPbPbPbP NuNuNuNuNuNuNuNu NuNuNuwRNuNuNuNu NuNuNuNuNuNuNuNu NuNuNuNuNuNuNuNu wPwPwPwPwPwPwPwP NuwNwBwQwKwBwNwR*/

所以,我正在用Java制作一个国际象棋引擎。给定当前的电路板配置,AI应该找出它可以做出的每个可能的动作,将其添加到所有可能动作的链接列表中,然后返回该列表。目前,我正在测试以下电路板配置:

/*bRbNbBbQbKbBbNbR
  bPbPbPbPbPbPbPbP
  NuNuNuNuNuNuNuNu
  NuNuNuwRNuNuNuNu
  NuNuNuNuNuNuNuNu
  NuNuNuNuNuNuNuNu
  wPwPwPwPwPwPwPwP
  NuwNwBwQwKwBwNwR*/
“bR”表示黑车,“bN”表示黑骑士,等等。“Nu”表示零或无碎片。在这个配置中,我将左下角的白车移动到板的中间

在我的
Mobility
类中,以下方法
possibleMoves()
应该生成并返回所有可能的电路板配置的链接列表。每个索引
i
对应于板上从左侧开始的部分。在这种情况下,AI是白色的,所以0是最左边的白色棋子,7是最右边的白色棋子,8是现在在棋盘中央的白色棋子,9是另一个白色棋子,等等。现在我只测试这些棋子,所以其他条件都是空的
nonPawnBoardGen()
返回可能的电路板配置的子列表

public LL possibleMoves(){
    LL children = new LL();
    /*
     * check each piece
     */
    for(int i = 0; i < 16; i++){
        if(i < 8){//pawns

        }
        else if(i < 10){//rooks
            children.joinWith(nonPawnBoardGen(i, 0, -1)); //positions to the left
            children.joinWith(nonPawnBoardGen(i, 0, 1)); //right
            children.joinWith(nonPawnBoardGen(i, -1, 0)); //up
            children.joinWith(nonPawnBoardGen(i, 1, 0)); //down
        }
        else if(i < 12){
        //  checkKnight(r, c, int dR, int dC)
        }
        else if(i < 14){//bishops

        }
        else{ //king, queen

        }
    }
    return children;
}
下面的函数,
nonPawnBoardGen()
,是我的Mobility中的另一个函数,它传递一个片段
索引和一个单位向量。因此,如果我想检查棋盘中央的所有可能的rook左移,我会调用
nonPawnBoardGen(8,0,-1)
,因为rook是索引8,它将保持在同一行,并在左边的列中迭代。该函数调用应该返回一个子列表,其中包含所有可能涉及该rook的板配置,因为我仍然需要从rook当前位置向右、向上和向下检查所有内容

private LL nonPawnBoardGen(int index, int vecR, int vecC){
    LL boardSubLst = new LL();
    int sR, sC; //source row and col
    if(turn == true){//white
        //last 16 coords are white pieces
        if(coords[index + 16] == null){//if piece does not exist, return
            return null;
        }
        sR = coords[index + 16].getRow(); //each coord is an object containing a row and col value
        sC = coords[index + 16].getCol();
    }
    else{//black
        //first 16 coords are black pieces
        if(coords[index] == null){
            return null;
        }
        sR = coords[index].getRow();
        sC = coords[index].getCol();
    }
    int curR = sR; //current row
    int curC = sC; //current col
    curR+=vecR; //iterate by unit vector
    curC+=vecC;
    while(curR > -1 && curR < 8 && curC > -1 && curC < 8){ //while in range of board
        if(turn == true){//white
            if(board[curR][curC].charAt(0) != 'w'){ //if space is free or opposite color, valid move
                coords[index + 16].setRow(curR); //move rook to new position
                coords[index + 16].setCol(curC);
                if(board[curR][curC].charAt(0) == 'b'){ //if space contains piece of opposite color,
                    int r, c;                           //piece needs to be removed
                    for(int j = 0; j < 16; j++){ //iterate through 16 opponent pieces
                        r = coords[j].getRow();
                        c = coords[j].getCol();
                        if(curR == r && curC == c){ //check which opponent's piece's coords match
                            coords[j] = null;       //the rook's current coords, then remove opp's piece
                            boardSubLst.insert(coords); //add board config to sublist
                            break;
                        }
                    }
                    break;
                }
                else{ //if the space is null, simply add board config to sublist
                    boardSubLst.insert(coords);
                }
            }
            else{ //if space is same color, break
                break;
            }
        }
        else{//black
            if(board[curR][curC].charAt(0) != 'b'){
                coords[index].setRow(curR);
                coords[index].setCol(curC);
                if(board[curR][curC].charAt(0) == 'w'){
                    int r, c;
                    for(int j = 0; j < 16; j++){
                        r = coords[j + 16].getRow();
                        c = coords[j + 16].getCol();
                        if(curR == r && curC == c){
                            coords[j + 16] = null;
                            boardSubLst.insert(coords);
                            break;
                        }
                    }
                    break;
                }
                else{
                    boardSubLst.insert(coords);
                }
            }
            else{
                break;
            }
        }
        curR+=vecR;
        curC+=vecC;
    }
    return boardSubLst;
}
并将其添加到电路板配置列表中:

boardSubLst.insert(coords);
但是,每次我编辑
coords
,boardSubList
链接列表中的每个值都会更改为
coords
的当前值。为什么会这样

编辑: 我想只要让
nonPawnBoardGen()
生成并只返回一组坐标,就可以避免这个问题。迭代器可以保存在类中,而不是本地保存在函数中。返回的每一组坐标都可以直接添加到
possibleMoves()
中的子项列表中。我会试试这个,看看你打电话时会发生什么…

boardSubLst.insert(coords);
您正在将相同的引用传递给
coords
数组。我认为您会发现最简单的解决方案是复制数组,例如使用

或者,假设coords的类型为
Coord[]
,您可以使用


我停止编写代码有一段时间,只是重新开始并解决了这个问题。谢谢你们,艾略特和阿南塔。你们都是对的。我的棋子坐标数组实际上是一个对象数组,每个对象都是一组坐标。最初,我更改了代码,以便在编辑坐标并将数组添加到列表之前,每次都制作一个数组副本,但这只是对同一组数组对象进行新引用。解决方案不仅是创建新的数组引用,而且在每次更改坐标时在数组中分配新的坐标对象。我觉得这听起来比实际情况要复杂得多,但是的,这是一个不断引用内存中相同区域的问题。这不太明显。非常感谢

应用程序正在传递LinkedList的引用,这是问题的原因,如果您需要评估可能的移动,请考虑调用<代码> toRayAuy()/Case>方法,这将返回未引用原始列表的数据副本。这完全是问题所在,所以谢谢您指出它。我想不出任何其他原因来解释为什么会发生这种情况。但是,我不能只对每一组坐标调用toArray(),因为它们不是列表,而是数组。至少我认为这就是原因。不过我有另一个主意。嗨,艾略特。谢谢你的回复。您确定的问题是有道理的,但两个建议的解决方案都不起作用。在这两种情况下,编辑坐标仍然会更改链表节点的值。
boardSubLst.insert(coords);
boardSubLst.insert(coords);
boardSubLst.insert(Arrays.copyOf(coords, coords.length));
Coord[] coords2 = new Coord[coords.length];   
System.arraycopy(coords, 0, coords2, 0, coords.length);
boardSubLst.insert(coords2);