-2

最近我试图在java中将hashmap元素推送到堆栈中,但是每次我将新元素推送到堆栈中时,堆栈中的所有元素都会被最近推送的元素替换。

这是我的代码:

state.previousState = dotPosColor;
state.pushState();
state.getElement();
public void pushState(){
    undos.push(previousState);
    Log.d("test","First->"+undos.firstElement().toString());
    Log.d("test","Last->"+undos.lastElement().toString());
    redos.clear();
}
4

1 回答 1

1

recently i am trying to push the hashmap element in the stack in java but every time i push the new element in the stack then all the elements in the stack is replaced with the recently pushed element.

I'm going to use my crystal ball and tell you that you are declaring your intermediate variable globally, when it needs to be local. ie:

for( some loop stuff here )
{
    Object o = hashMap.get("fwerin");
}
于 2013-07-31T10:31:55.403 回答