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.
}
}