Using list.remove((Object)93)
causes a ConcurrentModificationException
in this example:
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(14);
list.add(2);
list.add(44);
list.add(41);
list.add(93);
list.add(20);
for(Iterator<Integer> it = list.iterator();it.hasNext();) {
list.remove((Object)93);
it.next();
}
System.out.println(list);
However, when I wrap list.remove((Object)93)
in a if statement, it doesn't cause any errors. Why?
for(Iterator<Integer> it = list.iterator();it.hasNext();) {
int num = it.next();
if(num == 93) {
list.remove((Object)93);
}
}
System.out.println(list);