I want to copy the 'clues' array to the 'board' array only once. Why does the clues array change along with board after copying once?
public class Ejewbo
{
public static int[][] board = new int[9][9];
public static int[][] clues =
{
{0, 0, 0, 7, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 4, 3, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 6},
{0, 0, 0, 5, 0, 9, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 4, 1, 8},
{0, 0, 0, 0, 8, 1, 0, 0, 0},
{0, 0, 2, 0, 0, 0, 0, 5, 0},
{0, 4, 0, 0, 0, 0, 3, 0, 0},
};
public static void main(String[] args)
{
Ejewbo.board = Ejewbo.clues.clone();
test();
}
public static void printboth()
{
for (int j = 0; j < 9; j++)
{
for (int i = 0; i < 9; i++)
{
System.out.print(Ejewbo.board[j][i]);
System.out.print(" ");
}
System.out.println();
}
System.out.println();
for (int j = 0; j < 9; j++)
{
for (int i = 0; i < 9; i++)
{
System.out.print(Ejewbo.clues[j][i]);
System.out.print(" ");
}
System.out.println();
}
System.out.println("-----");
}
public static void test()
{
for (int i = 0; i < 2; i++) //run twice to see issue
{
Ejewbo.board[0][0]++;
printboth();
}
}
}
I would expect the clues array not to change, but it does. When a change is made to board, clues changes too. Why? Is there a better way to copy arrays like this (instead of using .clone())?
EDIT: The first answer here seems to be a good way for me to copy my arrays.