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?
You're making changes to the array - but you're not putting the array itself on the stack... you're putting a reference to the array on the stack. You're basically pushing the same reference several times. If you want independent values, you'll need independent arrays.
Think of a reference as being a bit like a street address written on a piece of paper (where the piece of paper is typically a variable). What you've got at the moment is a book where every page has the same street address on it. You may have gone and painted the front door of the house between writing on every page, but that doesn't mean we can see all those different colours - there's only one house involved. If you want to have the street addresses of lots of different houses with lots of differently-coloured front doors, you need to built lots of different houses...
See more on this question at Stackoverflow