How to avoid java.util.ConcurrentModificationException








The following code may cause java.util.ConcurrentModificationException:
for(Iterator it = list.iterator();it.hasNext();){
String member = (String)it.next();
if(member.equals("delete")){
list.remove(member);
break;
}
}
To work around:
String memberToDelete = null;
for(Iterator it = list.iterator();it.hasNext();){
String member = (String)it.next();
if(member.equals("delete")){
memberToDelete = member;
break;
}
}
if(memberToDelete!=null){
list.remove(memberToDelete);
}
 
See http://java.sun.com/j2se/1.5.0/docs/api/java/util/ConcurrentModificationException.html



No comments:

Post a Comment