-3

In this android game, I'm using an ArrayList to keep track of all of the entities currently on the screen. Running through the list allows me to successfully update and add entities to the screen / list, but attempting to remove them gives me a java.util.ConcurrentModificationException error. I'm a novice programmer, so I don't really know what could be going wrong.

The method I'm using to determine when a entity needs to be removed ( when it goes offscreen, but only through the top ) is this.

private void deleteEntities() {
    for(Entity ent : entsOnLevel) {
        if((ent.getY() + ent.getImage().getHeight()) < 0) {
            this.entsOnLevel.remove(ent);
        }
    }
}

Could someone explain to me what could be going wrong?

Beryllium
  • 12,808
  • 10
  • 56
  • 86

3 Answers3

3

You created an iterator and you are removing his current item, hence he cant find the next item.

Read more here -> ConcurrentModificationException?

Community
  • 1
  • 1
q99
  • 961
  • 2
  • 13
  • 27
  • Look at [the description of fail-fast](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#fail-fast). You can explicitly loop on the `Iterator` (instead of implicitly with an enhanced `for` loop) and call `it.remove()`. – chrylis -cautiouslyoptimistic- Aug 23 '13 at 08:41
  • this answer should be a close-as-duplicate vote – njzk2 Aug 23 '13 at 08:49
2

You should remove an element from a List using an Iterator.

    Iterator<YourDataType> it = YourList.iterator();
    while (it.hasNext()) 
           it.remove();

This should give you some hints, why you should use an Iterator.

Community
  • 1
  • 1
Steve Benett
  • 12,843
  • 7
  • 59
  • 79
1

You are facing ConcurrentModificationException because you are doing two operations on the same list at a time. i.e looping and removing same time.

Inorder to avoid this situation use Iterator,which guarantees you to remove the element from list safely ...

Ex:

List<Object> objs;
Iterator<Object> i = objs.iterator();
while (i.hasNext()) {
   Object o = i.next();
  //some condition
    i.remove();
}
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • Thank you! I knew there must have been something wrong with just straight up removing data from a list! I guess I should go read up on Iterators, huh? Should I use something similar for my update method, or is safe to do it the way I was trying to do the removal method? – Jason Jahn Aug 23 '13 at 08:44
  • @JasonJahn A thumb rule here is `If you are modifying a Collection object while looping on it`,use iterator. – Suresh Atta Aug 23 '13 at 08:50
  • It don't know if I'm using the iterator wrong, but using this method in place of my old one, with an enhanced for loop, stopped my entities from updating. while (it.hasNext()) { Entity ent = it.next(); ent.update(); } – Jason Jahn Aug 23 '13 at 09:28
  • In such case ,You are updating the entity ,Not collection.So do not use Iterator there.Use a normal foreach loop and update your entity.Updating collection means,Adding and /remove from it – Suresh Atta Aug 23 '13 at 09:32
  • That's odd, because remove from the collection doesn't seem to be working either. private void deleteEntities() { while (it.hasNext()) { Entity ent = it.next(); if(ent.getY() < 800) { it.remove(); } } } I've never used stackoverflow before, is there somewhere else I should be discussing this? – Jason Jahn Aug 23 '13 at 09:43