I debugged the code, and saw that every time I add a new element to the ArrayList
, all previous elements gets replaced by the new element. There are no static fields or methods in this class or the associated class. What could I be doing wrong? Thanks in advance!
** UPDATE ** GameState gs's field and everything is correct, and is what I want. But every time adding to childrenList, all previous elements get replaced with this current one.
For example -
1st iteration - gs = 1
[1]
2nd iteration - gs = 2
[2, 2]
3rd iteration - gs = 3
[3, 3, 3]
what I want is [1, 2, 3]
My code looks like this...
public List getChildren() {
ArrayList<GameStateChild> childrenList = new ArrayList<GameStateChild>();
List<Map<Integer, Action>> act = getActionPairs();
for (Map<Integer, Action> action : act) {
GameState gs = executeAction(action);
childrenList.add(new GameStateChild(action, gs));
}
return childrenList;
}