I tested the next()
and remove()
methods of the Iterator
interface. I got the following exception:
Exception in thread "main" java.util.ConcurrentModificationException
Here is my code:
import java.util.*;
public class ListTest {
public static void main(String[] args) {
Collection<Integer> list = new ArrayList<Integer>();
Iterator<Integer> iterator = list.iterator();
Collections.addAll(list, 1, 2, 3, 4, 5);
if (iterator.hasNext()) {
iterator.next();
iterator.remove();
}
System.out.println(list);
}
}