1

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;

}

  • 2
    This code will effectively add `GameStateChild`'s to your list. The problem is elsewhere. – Bifz Feb 27 '16 at 20:17
  • I dont understand why a method call "get" modify some element – user43968 Feb 27 '16 at 20:19
  • Can you explain that what you mean by replaced by the new one. Try adding some sample outout /input based on whome you are saying that they are replaced. – PyThon Feb 27 '16 at 20:23
  • 1
    can you show us the code that print your debug info?? – user43968 Feb 27 '16 at 20:36
  • Possible duplicate of [Why does my ArrayList contain N copies of the last item added to the list?](http://stackoverflow.com/questions/19843506/why-does-my-arraylist-contain-n-copies-of-the-last-item-added-to-the-list) – user43968 Feb 27 '16 at 20:55
  • Try posting the construct of your GameStateChild and GameState class, also a small field overview too. – PyThon Feb 27 '16 at 21:11

2 Answers2

1

Here is what's probably happening: Your game state objects all refer to the SAME game state object. Changing the value of one of them changes all of them.

In other words, it's likely that somewhere deep in your code something akin to this is happening:

GameState gs1 = returnsAGameState();
GameState gs2 = gs1;
changeThisGameState(gs2);

In this code, gs1 and gs2 refer to exactly the same location in your computer. As a result, any change to gs2 is also a change to gs1.

The code that you posted doesn't seem to have any problems. I would guess that the problem comes from the call to executeAction(gs) method -- try looking there.

NcAdams
  • 2,521
  • 4
  • 21
  • 32
  • what would be the correct way to make a copy of a GameState object if this were the case? – user2444784 Feb 27 '16 at 21:02
  • 1
    Turns out, you were right! Problem is fixed now. Thank you so much!!! – user2444784 Feb 27 '16 at 21:16
  • What was the cause ?? – PyThon Feb 27 '16 at 21:18
  • @NcAdams- how can all refers to the same object if they were created via new keyword. They may have the same data inside (logic hidden in constructor or executeAction method()) but they will be whole new objects. – PyThon Feb 27 '16 at 21:24
  • I'm not really sure what was the cost. But GameState has a lot of fields, so instead of doing something like MapLocation newLoc = oldGameState.loc, I did newLoc.x = oldGameState.loc.x and that seemed to solve the problem. – user2444784 Feb 27 '16 at 21:45
  • @PyThon Only "GameStateChild" is created with the "new" keyword. So you're right, the GameStateChild objects were all referring to different objects. It sounds like the problem was that each individual GameStateChild referred to the same GameState. – NcAdams Feb 27 '16 at 23:28
0

Check if gs variable inside GameState is non static.

I wonder if this is really happening. ArrayList's add method works only on addreses and you are inserting new object each time so it should not ovveride the previous ones unless you manually changes them.

PyThon
  • 1,007
  • 9
  • 22