我有这个Java代码:
boolean[][] defaultBoard = {{false, false, false},
{false, false, false},
{false, false, false}};
Board board = new Board(defaultBoard);
Board nextBoard = new Board(defaultBoard);
nextBoard.getBoard()[1][5] = true;
board.printBoard();
printBoard
:
public void printBoard() {
for (boolean row[] : board) {
for (boolean cell : row) {
System.out.print( cell ? "#" : "." );
}
System.out.println();
}
System.out.println();
}
但它返回
...
.#.
...
这让我想到了,nextBoard
并且board
得到了相同的内存地址。我的问题是:如何创建副本nextBoard
以便在nextBoard
编辑时board
不进行编辑?