It's very clear that this code is modifying a list during iteration.
public class ArrayIterator {
public static void main(String[] args) {
List<String> list = new LinkedList<>(Arrays.asList("A","B","C","D","E"));
Iterator<String> it = list.iterator();
while (it.hasNext())
{
list.remove(it.next());
}
}
}
And so we get the expected Exception
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966)
at java.util.LinkedList$ListItr.next(LinkedList.java:888)
at ArrayIterator.main(ArrayIterator.java:15)
Why isn't the compiler able to warn about this?