In my project there is a monochromatic screen you can write to and clear. Within the class I have these two lines of code for clearing the screen. the first pushes the current pixel values from the screen onto a stack so the clearscreen can be undone later. The second line clears the screen. the problem is that the undo stack is getting a reference to this.pixels rather than getting the value of it at the time.
this.pushUndo(this.pixels); //this.pixels is an array of ints
this.updateScreen(new Array(64*32)); //this.pixels changes at the end of this line but the undo stack shouldn't change its value