【发布时间】:2016-03-27 16:02:23
【问题描述】:
我已经定义了一个类如下:
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),对一个板所做的任何更改都会影响另一个板。我复制了单独的原始元素,但对两个板的 matrix 的引用是相同的。我以为我在复制元素,为什么指针是一样的?我该怎么做才能解决这个问题?
【问题讨论】:
标签: java arrays multidimensional-array