I have function to deep copy a board of squares:
public Square[][] copy_array(Square[][] board) {
Square[][] nv = new Square[8][8];
for (int i = 0; i < nv.length; i++)
nv[i] = Arrays.copyOf(board[i], board[i].length);
return nv;
}
when I use this function and make changes to nv
, it makes a change to the original board[][] square
that I pass in. I read that Arrays.copyOf
returns a cloned array that is unaffected by changes to the original. but this does not work the other way round, like the example above. the memory reference in source array is just pointed to the new location as per: Does Arrays.copyOf produce a shallow or a deep copy?
what would be the best way to do a true deep copy so that changes to the objects in nv
do not change the objects in board
?