I'm working with an int[n][n] board:
0 2 3 4 1 5 7 8 6 I want to make a copy called twin, then return the modified copy.
For example:
int [][] twin = board.clone(); twin[0][0] = board[0][1]; twin[0][1] = board[0][0]; return twin; What I expected is:
//board 0 2 3 4 1 5 7 8 6 //twin 2 0 3 4 1 5 7 8 6 But the result is:
//board 2 2 3 4 1 5 7 8 6 //twin 2 2 3 4 1 5 7 8 6 The board and twin were the same all along and the clone was not working as expected. Is it because int[][] is not an object?
Is there a way to clone int[][] and modify the way I was expecting? Should I loop over board and copy the values to twin?