1

Actually after pressing a button i have a method which check if in my itemStorico ArrayList exist new added items from itemModel, then if the item exist in the new list called itemModel i'm removing the old item from itemStorico and adding the new one, else just adding the new item without removing the old.

But i'm getting the following error:

2018-12-11 16:34:04.067 29033-29033/it.gabtamagnini.realco E/AndroidRuntime: FATAL EXCEPTION: main
    Process: it.gabtamagnini.realco, PID: 29033
    java.util.ConcurrentModificationException
        at java.util.ArrayList$Itr.next(ArrayList.java:831)
        at it.gabtamagnini.realco.OrdiniActivity.saveStorico(OrdiniActivity.java:1396)
        at it.gabtamagnini.realco.OrdiniActivity$29.onClick(OrdiniActivity.java:1368)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22433)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6130)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

While here is the code from my method:

 for(ItemModel itemModels : itemModel){
            boolean exist = false;
            for(ItemModel itemModel2 : itemStorico){
                if(itemModels.getCodiceArticolo().contains(itemModel2.getCodiceArticolo())) {
                    itemStorico.remove(itemModel2);
                    itemStorico.add(itemModels);
                    exist = true;
                }
            }
            if(!exist) {
                itemStorico.add(itemModels);
            }
        }
NiceToMytyuk
  • 3,644
  • 3
  • 39
  • 100

2 Answers2

4

You cannot call remove() from inside a "for-each" loop (the for (item : collection) structure). Doing so will throw that ConcurrentModificationException.

If you want to loop and remove items while looping, you can use a traditional "for" loop:

for (int i = 0; i < itemStorico.size(); ++i) {
    ItemModel itemModel2 = itemStorico.get(i);
    if (...) {
        itemStorico.remove(i);
        ...
    }
}

However, this will introduce a subtle bug. Imagine you're looping over [a,b,c,d]. If you are at index 1 and remove b from the collection, everything will "slide over" and you'll have [a,c,d]. When you next look at index 2, you'll have skipped over c.

You can avoid the bug by manually decrementing the index when you remove, but it's a little gross.

...
    itemStorico.remove(i);
    --i;
...

You can also avoid the problem altogether by using the collection's iterator().

for (Iterator<ItemModel> iterator = itemStorico.iterator(); iterator.hasNext(); ) {
    ItemModel itemModel2 = iterator.next();
    if (...) {
        iterator.remove();
        ...
    }
}
Ben P.
  • 52,661
  • 6
  • 95
  • 123
0
for(ItemModel itemModel2 : itemStorico){
            if(itemModels.getCodiceArticolo().contains(itemModel2.getCodiceArticolo())) {
                itemStorico.remove(itemModel2);

You are removing an element from a list while iterating through it. You can't!

To avoid this concurrence problem you can use an iterator, but check this answer that explains all you need to know: Remove elements from collection while iterating