0
 boolean[][] grid = {{false,false},
                     {false,false}};
 WorldState test1 = new WorldState(grid,0,0);
 System.out.println(Arrays.deepToString(grid));

为什么我在 WorldState 中使用 in 后网格的布尔值会发生变化。它不应该保持错误,因为我没有为网格分配任何东西。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;
     }
 }
4

1 回答 1

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;
 }
于 2013-11-02T00:57:02.477 回答