boolean[][] grid = {{false,false},
{false,false}};
WorldState test1 = new WorldState(grid,0,0);
System.out.println(Arrays.deepToString(grid));
为什么在WorldState中使用后,网格的布尔值会发生变化。不应该保持虚假,因为我没有给网格分配任何东西。 I系统打印出网格,它是[[true,false],[false,false]]。我不明白真实的来源。请告诉我为什么它会变为真实。谢谢。下面的世界州代码:
public class WorldState
{
boolean[][]grid2;
public WorldState(boolean[][] grid, int row, int col)
{
this.grid2=grid;
this.grid2[row][col] = true;
}
}
答案 0 :(得分:0)
这一切都是引用相同的网格this.grid2=grid;
。
试试这个
boolean[][]grid2;
public WorldState(boolean[][] grid, int row, int col)
{
int gridRow = grid.length;
int gridCol = grid[0].length;
grid2 = new boolean[gridRow][gridCol];
for (int i = 0; i < gridRow; i++) {
for (int j = 0; j < gridCol; j++){
grid2[i][j] = grid[i][j];
}
}
grid2[row][col] = true;
}