I wrote this example following the ConcurrentModificationException test:
public class Person { String name; public Person(String name) { this.name = name; } } public static void main(String[] args) { List<Person> l = new ArrayList<Person>(); l.add(new Person("a")); l.add(new Person("b")); l.add(new Person("c")); int i = 0; for(Person s : l) { if(s.name.equals("b")) l.remove(i); i++; } for(Person s : l) System.out.println(s.name); }
When I executed the above main method, ConcurrentModificationException will not be thrown, and the output console will output the following result:
a c
As far as I know about this problem, when in the loop for the list, when changing the list, you should throw a ConcurrentModificationException . But why doesn’t this happen in my example?
source share