0

I am using the following code to create a CopyOnWriteArrayList, modify the elements residing in it and then printing the elements. I am not able to understand why I am getting ConcurrentModificationException when I am using CopyOnWriteArrayList.

public class Solution {
    public static int main() {
          CopyOnWriteArrayList<Integer> arrList = new CopyOnWriteArrayList<>();

          //fill the array list;
          List<Integer> sublist = arrList.sublist(startIndex, endIndex);
          arrList.removeAll(sublist);
          arrList.addAll(sublist); //here is where the ConcurrentModificationException comes and I dont understand why

          //iterate and print the elements of the list.
    }      
}
Varun Bhatia
  • 4,326
  • 32
  • 46
  • 1
    I suspect it is because `sublist` gives you a list that is *backed by the original list*. – RealSkeptic Mar 23 '17 at 14:01
  • 2
    [copyonwritearraylist-throwing-currentmodificationexception](https://stackoverflow.com/questions/1527519/copyonwritearraylist-throwing-currentmodificationexception) – HDJEMAI Mar 23 '17 at 14:05

0 Answers0