I have a 2 dimensional array and stack:
int[][] data = new int[][];
Stack<int[][]> undoList = new Stack<int[][]>(10);
I wanna add the data after every change to keep it in undo list:
undoList.push(data);
// some changes to data array
undoList.push(data);
// some changes to data array
undoList.push(data);
then why do all the stack elements have the same value?
I solved it by copying it with this code as @Marco13 has mentioned in the comment above:
Deep Copy
public static int[][] deepCopyArray(int[][] oldArray){
int[][] newArray = new int[oldArray.length][];
for(int i=0; i<oldArray.length; i++)
{
newArray[i] = new int[oldArray[i].length];
for(int j=0; j<oldArray[i].length; j++)
newArray[i][j] = oldArray[i][j];
}
return newArray;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments