I've been trying to take a sub list of a list, reverse it, and place the reversed list back into the starting position. For example, say we have the list [1, 2, 3, 4, 5, 6]
, then reversing from index 2 to index 4 would give [1, 2, 5, 4, 3, 6]
.
I've written some code for this, however it gives a ConcurrentModificationException
every time (unless startIndex == endIndex). A minimum reproducible example is provided below:
int startIndex = 2;
int endIndex = 4;
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
List<Integer> toReverse = list.subList(startIndex, endIndex+1);
Collections.reverse(toReverse);
list.removeAll(toReverse);
list.addAll(startIndex, toReverse);
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.ArrayList$SubList.checkForComodification(Unknown Source)
at java.util.ArrayList$SubList.size(Unknown Source) at
java.util.AbstractCollection.toArray(Unknown Source) at
java.util.ArrayList.addAll(Unknown Source) at
test.ConcurrentExample.main(ConcurrentExample.java:64)
The actual line the error refers to is list.addAll(startIndex, toReverse);
.
I'm not sure what the issue is as nothing appears to be getting changed while being iterated over. If anyone could explain why this is happening and/or how to fix it, that would be greatly appreciated.