0

I have ClassA which has a static ArrayList of Objects

public static ArrayList<Meteorit> meteorits = new ArrayList<Meteorit>();

Now I want to remove an object from this list like this

ClassA.meteorits.remove(this);

This is written in Meteorit class . But it throws exception when I want to use the objects in the ArrayList .

Exception in thread "LWJGL Application" java.util.ConcurrentModificationException

I used Iterator to remove objects from ArrayList but now I dont have an idea how to use it in this case.

Vahe Muradyan
  • 1,115
  • 1
  • 11
  • 22
  • 1
    possible duplicate of [Remove elements from collection while iterating](http://stackoverflow.com/questions/10431981/remove-elements-from-collection-while-iterating) – John May 23 '15 at 08:31
  • Why is the ArrayList `static`? I suggest to do it exactly like how `observer` design pattern did. First, make a method inside `ClassA` call it `public removeMetorit(Meteorit meteorit)` and use it from wherever exept the `Meteorit` class. – MChaker May 23 '15 at 08:39
  • Also, you are calling the `remove` method from inside the object to be removed and this is illegal. – MChaker May 23 '15 at 08:42
  • I want the meteorite to remove itself from the list, I don't think it's illegal. – Vahe Muradyan May 23 '15 at 09:42

4 Answers4

3

It is because some thread is actually viewing this list in a for each loop, maybe you are trying to remove elements of this list in the body of for-each? You can't remove elements in for-each, but you can in iterator loops:

You can use iterator instead of for each to remove and view the elements of the list like this:

public static ArrayList<Meteorit> meteorits = new ArrayList<Meteorit>();

Iterator<Meteorit> itr = meteorits.iterator();

while (itr.hasNext()) {
       if (itr.next()==this) {
          itr.remove();
       }
}
Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
1

When using an iterator; you need to use the iterator to remove items from a list using this:

iterator.remove();

which from the Java Docs says:

Removes from the underlying collection the last element returned by this iterator.

Removing a item from the list by any other means will result in the ConcurrentModificationException you are seeing.

Brett Walker
  • 3,566
  • 1
  • 18
  • 36
1

Basically, you need to use iterator to avoid such concurrent modification:

List<String> list = new ArrayList<>();
for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
    String string = iterator.next();
    if (string.isEmpty()) {
        iterator.remove();
    }
}

For more details, please check out this post:

Iterating through a Collection, avoiding ConcurrentModificationException when removing in loop

Community
  • 1
  • 1
Hoa Nguyen
  • 13,452
  • 11
  • 45
  • 44
0
Iterator<Integer> itr = yourlist.iterator();

// remove all even numbers
while (itr.hasNext()) {
       itr.remove();
}

this must work for you, other way to work around this issue is to use CopyOnWriteArrayList hopefully it help.

Alaa Abuzaghleh
  • 1,023
  • 6
  • 11