I read that the correct way of removing elements while iterating the Collection, is this way (using iterator):
List<Integer> list = new ArrayList<Integer>();
list.add(12);
list.add(18);
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
itr.remove();
}
But, I receive Exception in thread "main" java.lang.IllegalStateException
and I don't know why.
Can someone help me?