0
ArrayList<Integer> arrCopy = new ArrayList<>(arr);

for (List<Integer> operation : operations) {
    List<Integer> leftSub = arrCopy.subList(0, operation.get(0));
    List<Integer> middleSub = arrCopy.subList(operation.get(0), operation.get(1)+1);
    List<Integer> rightSub = arrCopy.subList(operation.get(1), arr.size()-1);

    Collections.reverse(middleSub);

    arrCopy.clear();
    arrCopy.addAll(leftSub);
    arrCopy.addAll(middleSub);
    arrCopy.addAll(rightSub);
}

Irrelevant of what this piece of code is trying to achieve, why is it giving a ConcurrentModificationException? I thought that exception was thrown when you try to modify the Collections you are iterating over. In my example, that's not the case. So, what's the reason of this exception and how may I solve it?

0 Answers0