I have the following code:
// Creates ArrayList with integers 0-9
ArrayList<Integer> intKeys = new ArrayList<Integer>();
for (int x = 0; x < 10; x++) {
intKeys.add(x);
}
// Creates iterator with integers 0-9
Iterator<Integer> allKeys = intKeys.iterator();
// Loops 10 times, each time obtaining a key to remove from the ArrayList
for (int x = 0; x < 10; x++) {
Integer key = allKeys.next();
intKeys.remove(key);
}
I understand that ConcurrentModificationException is thrown if an element of a collection is removed while the collection is being looped over, such as:
for (String key : someCollection) {
someCollection.remove(key);
}
but right now I'm looping over nothing - just an arbitrary number of times. Furthermore, the error line is interestingly Integer key = allKeys.next();
What could be the cause of this exception?