Reputation: 191
I have this Java code:
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();
}
But it returns
...
.#.
...
Which makes me think that nextBoard
and board
have got the same memory address. My question is: How do create a copy of nextBoard
so that when nextBoard
is edited, board
isn't edited as well?
Upvotes: 1
Views: 157
Reputation: 576
You need to create a new fresh array every time you need to pass it to object so that the object's see a new array rather then the same array .
Upvotes: 0
Reputation: 533442
In this case, I would have the board create the array base don a size
class Board {
final boolean[] board;
public Board(int width, height) {
board = new[width][height];
}
public boolean[][] getBoard() { return board; }
public void set(int x, int y, boolean value) { board[x][y] = value; }
}
Now your code looks like this
Board board = new Board(3, 3);
Board nextBoard = new Board(3, 3);
nextBoard.set(1, 2, true); // the last element is 2 not 5
board.printBoard();
Upvotes: 0
Reputation: 4252
It's not board
and nextBoard
that are the same. It is the defaultBoard
array you are passing to the constructor. You need to pass different arrays. Otherwise, you are just modifying the same array.
Better yet, to ensure instances of Board
always contain a new array, I would recommend doing a deep copy of the array inside the constructor.
Upvotes: 4
Reputation: 2785
An array is an object, so you will pass a reference to the Board
constructor. Since you are using the same array, both board
and nextBoard
are sharing the same object. To avoid this situation, use the clone
method. This way, you will have a copy of the defaultBoard
array and every instance you have their own board. It will look like this:
Board board = new Board(cloneBoard(defaultBoard));
Board nextBoard = new Board(cloneBoard(defaultBoard));
private boolean[][] cloneBoard(boolean[][] boardToClone) {
boolean[][] clone = new boolean[boardToClone.length][];
for(int i = 0; i < boardToClone.length; i++) {
clone[i] = boardToClone[i].clone();
}
return clone;
}
Upvotes: 0