Java 无法添加到ArrayList

Java 无法添加到ArrayList,java,arrays,list,arraylist,Java,Arrays,List,Arraylist,我试图遍历一个列表“breadthBoard”,并在其中添加一个数组“board”。但是,我添加到数组中的每个数组都会以某种方式转换为原始数组,然后进行复制,即使我已经测试过数组已经更改 neighbourNodes是一个列表,包含电路板上与currentNode相邻的所有值 public List breadthBoard(List neighbourNodes, int [] currentNode, int [][] board) { int x = currentNode[0]

我试图遍历一个列表“breadthBoard”,并在其中添加一个数组“board”。但是,我添加到数组中的每个数组都会以某种方式转换为原始数组,然后进行复制,即使我已经测试过数组已经更改

neighbourNodes是一个列表,包含电路板上与currentNode相邻的所有值

public List breadthBoard(List neighbourNodes, int [] currentNode, int [][] board)
{

    int x = currentNode[0] - 1; 
    int y = currentNode[1] - 1;
    //create lists
    List breadthBoard = new ArrayList();

for (int i=0; i<3;i++)
        {
            for(int j=0; j<3;j++)
            {

                if (neighbourNodes.contains(board[i][j]))
                {
                    // the temp variables allow me to switch the values then switch back later on
                    int temp = board[i][j];
                    int temp2 = board[x][y];
                    //initial switch
                    board[i][j] = temp2;
                    board[x][y] = temp;// at this point I get a successful swap but it isn't getting added to the breadth board
                    breadthBoard.add(board);

                    //test to see if I get the right results which I do
                    System.out.println("what's being added into breadth board (should be swapped)" + Arrays.deepToString(board)); 
                    System.out.println('\n');

                    switching the values back to the original postions
                    board[i][j] = temp;
                    board[x][y] = temp2;
                    System.out.print("back to normal " + Arrays.deepToString(board));// another test passed
                    System.out.println('\n');

                } 

            }
公共列表扩展板(列表相邻节点,int[]currentNode,int[]board)
{
int x=当前节点[0]-1;
int y=当前节点[1]-1;
//创建列表
列表宽度板=新的ArrayList();

对于(int i=0;i您需要制作一个方法,该方法制作
板的深度副本
克隆
仅制作浅副本

我建议将电路板包装在一个类中,该类有一个
swap
方法,只返回一个新的电路板:

class Board {
    private final int[][] board;

    public Board(Board other) {
        this.board = new int[other.board.length][];
        for(int i = 0; i < board.length; i++) {
            board[i] = Arrays.copyOf(other.board[i], other.board[i].length);
        }
    }

    public Board swap(int i, int j, int x, int y) {
        Board result = new Board(this); // copy this board

        // swap elements    
        result.board[i][j] = board[x][y];
        result.board[x][y] = board[i][j];

        return result;
    }

    ...

}

您需要制作一种方法,使
线路板
克隆
只制作浅拷贝

我建议将电路板包装在一个类中,该类有一个
swap
方法,只返回一个新的电路板:

class Board {
    private final int[][] board;

    public Board(Board other) {
        this.board = new int[other.board.length][];
        for(int i = 0; i < board.length; i++) {
            board[i] = Arrays.copyOf(other.board[i], other.board[i].length);
        }
    }

    public Board swap(int i, int j, int x, int y) {
        Board result = new Board(this); // copy this board

        // swap elements    
        result.board[i][j] = board[x][y];
        result.board[x][y] = board[i][j];

        return result;
    }

    ...

}
我做了一些测试。 事实上,
clone()
使用基元类型数组进行深度复制,但与二维基元数组的工作方式不同。
对于具有两个维度的数组,应在第一个维度上迭代,并在第二个维度的
int[]
数组上执行
clone()

   board[i][j] = temp2;
   board[x][y] = temp;

   //Here, I suppose that the second dimension has always the same size. 
   int[][] clonedBoard = new int[board[0].length][board[1].length];

   for (int t = 0; t < board.length; t++) {
     clonedBoard[t] = board[t].clone();
   }
   breadthBoard.add(clonedBoard);
board[i][j]=temp2;
板[x][y]=温度;
//在这里,我假设第二维度的大小总是相同的。
int[]clonedBoard=new int[board[0].length][board[1].length];
对于(int t=0;t
我做了一些测试。 事实上,
clone()
使用基元类型数组进行深度复制,但与二维基元数组的工作方式不同。
对于具有两个维度的数组,应在第一个维度上迭代,并在第二个维度的
int[]
数组上执行
clone()

   board[i][j] = temp2;
   board[x][y] = temp;

   //Here, I suppose that the second dimension has always the same size. 
   int[][] clonedBoard = new int[board[0].length][board[1].length];

   for (int t = 0; t < board.length; t++) {
     clonedBoard[t] = board[t].clone();
   }
   breadthBoard.add(clonedBoard);
board[i][j]=temp2;
板[x][y]=温度;
//在这里,我假设第二维度的大小总是相同的。
int[]clonedBoard=new int[board[0].length][board[1].length];
对于(int t=0;t
您从哪里获得
x
y
以及相邻节点是什么?我们需要更多地了解正在发生的事情。面包板声明在哪里,它的类型是什么?我将添加方法的其余部分。您多次添加相同的数组。使用
add
不会生成数组的深度副本。这看起来像一个重复我已经添加了在何处声明面包板以及所有方法参数。您从何处获得
x
y
,以及相邻节点是什么?我们需要更多地了解发生了什么。面包板在何处声明,它的类型是什么?我将添加其他方法。您正在添加相同数组的时间是多少使用
add
不会生成数组的深度副本。这看起来像是我添加的一个副本,其中声明了面包板和所有方法参数:)没问题。就信息而言,使用这种方法,不再需要交换。想法是先克隆阵列,然后直接修改克隆的阵列。:)没问题。就信息而言,使用这种方法,不再需要交换。想法是先克隆阵列,然后直接修改克隆的阵列。