我正在使用以下代码创建一个 CopyOnWriteArrayList,修改其中的元素,然后打印这些元素。我无法理解为什么在使用 CopyOnWriteArrayList 时会出现 ConcurrentModificationException。
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.
}
}