I have this code:
public static void main(String[] args) {
User user = new User("user1","user1",1l);
User user1 = new User("user2","user2",2l);
User user2 = new User("user3","user3",3l);
List<User> list = new ArrayList<User>();
list.add(user);
list.add(user1);
list.add(user2);
for(User user3 : list){
System.out.println(user3.getName());
if(user3.getName().equals("user1")){
list.remove(user3);
}
}
}
when executing this code , I get the following error:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
How can I avoid it?