1

I'm adding/removing Items to/from ArrayLists in my app in the following way:

public long gotoItem(int pos) {
  if (pos > 0) {
      List<QueueItem> mSubList = mItems.subList(0, pos);

      switch (mRepeatType) {
          case NO_REPEAT:
          case REPEAT_SINGLE:
              mPlayedItems.addAll(mSubList);

              mItems.removeAll(mSubList);
              break;
          case REPEAT_ALL:
              mItems.removeAll(mSubList);

              mItems.addAll(mSubList);
              break;
      }
  }

  return (long) mItems.get(0).getItem();
}

I get the following exception thrown:

> java.util.ConcurrentModificationException at java.util.AbstractList$SubAbstractList.listIterator(AbstractList.java:314)
                                                               at java.util.AbstractList$SubAbstractList.iterator(AbstractList.java:301)
                                                               at java.util.AbstractCollection.contains(AbstractCollection.java:127)
                                                               at java.util.AbstractCollection.removeAll(AbstractCollection.java:277)
                                                               at at.guger.musixs.playback.queue.Queue.gotoItem(Queue.java:135)
                                                               at at.guger.musixs.playback.MusicService.gotoPosition(MusicService.java:500)
                                                               at at.guger.musixs.playback.MusicPlayer.gotoPosition(MusicPlayer.java:138)
                                                               at at.guger.musixs.activity.MainActivity.SyncLocation(MainActivity.java:707)
                                                               at at.guger.musixs.activity.MainActivity.access$200(MainActivity.java:73)
                                                               at at.guger.musixs.activity.MainActivity$1.onServiceConnected(MainActivity.java:605)
                                                               at at.guger.musixs.playback.MusicPlayer$ServiceBinder.onServiceConnected(MusicPlayer.java:246)
                                                               at android.app.LoadedApk$ServiceDispatcher.doConnected(LoadedApk.java:1224)
                                                               at android.app.LoadedApk$ServiceDispatcher$RunConnection.run(LoadedApk.java:1241)
                                                               at android.os.Handler.handleCallback(Handler.java:739)
                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                               at android.os.Looper.loop(Looper.java:148)
                                                               at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Why does this error occur here and how can I fix this error in the simplest way?? - Do I really have to make a loop removing (and adding) the items from/to the arraylists?

Thank you!

the_dani
  • 2,466
  • 2
  • 20
  • 46
  • Duplicate of https://stackoverflow.com/questions/8104692/how-to-avoid-java-util-concurrentmodificationexception-when-iterating-through-an?rq=1 – Ivan Pronin Jun 28 '17 at 20:03
  • @IvanPronin I know this question but I'm not really looking for an iterator example if not really necessary! – the_dani Jun 28 '17 at 20:10

1 Answers1

8

SubList is actually a view on top of the original list. Internally it still holds the original list. So when you call originalList.removeAll(subList), internally it actually iterates over the original list and also tries to modify it. This is why you get ConcurrentModificationException.
A simple solution is to copy the sub-list to a new list (simplified example):

List<QueueItem> subList = new ArrayList<>(items.subList(0, pos));
items.removeAll(subList);
yinon
  • 1,418
  • 11
  • 14