Frank
Frank

Reputation: 740

java.util.ConcurrentModificationException when modifying list

I'm trying to delete some files in my database using App Engine and objectify. This the piece of code where I have the exception:

for(SottoCategoria sc: lsc){
      List<Key<Dispensa>> ld2=sc.getDispense();
      if(ld2!=null){
         for(Key<Dispensa> kd : ld2){ // <---- Exception!!
            if(kd.equals(k)){
                sc.removeDispensa(k);
                ofy().save().entity(sc).now();
                                    break;
            }
        }
    }
}

And this is the method that remove from the list:

public void removeDispensa(Key<Dispensa> k ){
    Iterator<Key<Dispensa>> kIter = dispense.iterator();
    while(kIter.hasNext()) {
        Key<Dispensa> kk = kIter.next();
        System.out.println(kk.equals(k));
        if(kk.equals(k)){
            kIter.remove();
            break;
        }
    }

}

How can I resolve it? Thanks!

Edit:

I resolved inserting a simple break in the loop, because in Sottocategoria can be only one dispensa that i want to remove!

Upvotes: 2

Views: 4275

Answers (5)

Rafa Romero
Rafa Romero

Reputation: 2716

your problem is because you are trying to modify the same array that you are reading, so to avoid the problem yo must replicate your array and iterate the duplicate array and doing the operations in the good one.

Tell me if you need some help with the code

Upvotes: 0

kai
kai

Reputation: 6887

ConcurrentModificationException occurs because you are try to delete a part of your list, while you are iterating through it. Try it with a for loop with index or creat a iterator.

example for-loop:

    for(int i = 0; i<dispense.size();i++) {
        System.out.println(dispense.get(i).equals(k));
        if(dispense.get(i).equals(k)){
            dispense.remove(i);
            break;
        }
    }

example iterator:

    Iterator<Key<Dispensa>> It = dispense.iterator();
    while(It.hasNext()) {
        Key<Dispensa> kk = It.next();
        System.out.println(kk.equals(k));
        if(kk.equals(k)){
            dispense.remove(kk);
            break;
        }
    }

Upvotes: 3

Keerthivasan
Keerthivasan

Reputation: 12890

Try to do it with List.iterator() to avoid this exception.

ConcurrentModificationException - This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible. This will not happen when you go for an iterator.Using an Iterator to iterate through a Collection is the safest and fastest way to traverse through a Collection

Upvotes: 1

Memran
Memran

Reputation: 405

Iterators are more suited to this use case.

Here is an example:

public void removeDispensa(Key<Dispensa> k ){
    Iterator<Dispensa> kIter = k.iterator();
    while(kIter.hasNext()) {
        Dispensa kk = kIter.next();
        System.out.println(kk.equals(k));
        if(kk.equals(k)){
            kIter.remove();
        }
    }
}

Upvotes: 2

Jannis Alexakis
Jannis Alexakis

Reputation: 1319

You can´t modify the list you are iterating through.

Upvotes: 1

Related Questions