a)引起麻烦的部分方法
public static void place2(String output, int position,furniture furniture_array[], char room_grid[][]) {
int i=0;
int j=0;
furniture_array[position].setXY(i, j);
char grid1[][] = room_grid;
char grid2[][] = room_grid;
char grid3[][] = room_grid;
char grid4[][] = room_grid;
pasteToArray(i, j, grid1, furniture_array[position]);
}
b)中pasteToArray
这是接收input_array的非常简单的方法,在这个数组中它将超过作为对象值的数组。
public static char[][] pasteToArray(
int x, int y, char input_array[][], furniture furniture_to_be_placed) {
char[][] result=input_array;
for (int i = 0; i <= furniture_to_be_placed.getSize(); i++) {
for (int j = 0; j <= furniture_to_be_placed.getSize(); j++) {
result[x + i][y + j] = furniture_to_be_placed.furn_grid[i][j];
}
}
return result;
}
我的问题是什么?
我自己已经做了什么来解决它: -
任何人都可以帮助我吗?
答案 0 :(得分:5)
grid1,grid2,grid3和grid4都是对同一个二维数组的引用。更改一个值将改变所有值。
此代码......
char grid1[][] = room_grid;
char grid2[][] = room_grid;
char grid3[][] = room_grid;
char grid4[][] = room_grid;
不会创建4个room_grid副本,而是创建对同一内存块的4个不同引用。