Given this smaller scale of what I am trying to achieve in Java:
int[][] arr = new int[2][2]{
{ 1, 0 },
{ 0, 1 }
};
int[][] path = null;
Queue<int[][]> q = new LinkedList<int[][]>();
q.add(arr);
while(q.size() != 0) {
path = q.poll(); // pop the queue
for (int i=0; i<2; i++) {
for (int j=0; j<2; j++) {
if (path[i][j] == 0) {
path[i][j] = 1;
q.add(path);
path[i][j] = 0;
}
}
}
}
I had traced the contents of q for each iteration inside the while and for loops via NetBeans. They are, however, of the same values.
I was expecting this on the end for the first for-loop execution:
{ {1 , 1} , {0 , 1} } and { {1 , 0} , {1 , 1} }
But instead, they are:
{ {1 , 0} , {0 , 1} } and { {1 , 0} , {0 , 1} }
which is the last value of the array path, as the 1st execution of the nested for-loop has ended.
What should I do so that the one that I am pushing to q is not a reference to the path array?