2
我定义一个类,如下所示:如何在Java中的对象内复制二维数组?
Public class Board{
public static final int SIZE = 4;
private static char[][] matrix = new char[SIZE][SIZE];
public Board(){
clear();//just fills matrix with a dummy character
}
public void copy(Board other){//copies that into this
for(int i = 0; i < SIZE; i++){
for(int j = 0; j < SIZE; j++){
matrix[i][j] = other.matrix[i][j];
}
}
}
//a bunch of other methods
}
因此,这里是我的问题:当我尝试做一个副本,像myBoard.copy(otherBoard)
,一个板所做的任何更改影响其他。我复制了各个原始元素,但对两个Board的参考matrix
是相同的。我以为我是复制元素,为什么指针是一样的?我能做些什么来解决这个问题?
谢谢你,我觉得很荒谬。我的CS老师不是很好,但你是 – LeoShwartz
@LeoShwartz谢谢你,但是太荣幸了。 ; d –