public class TestArrayList {
public static void main(String[] args) {
ArrayList al=new ArrayList();
al.add("java01");
al.add("java02");
al.add("java03");
Iterator itr=al.iterator();
while(itr.hasNext())
{
String a=(String)itr.next();
if(a.equals("java01"))
al.add("asd");
}
}
}
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(Unknown Source)
at java.util.AbstractList$Itr.next(Unknown Source)
at TestArrayList.main(TestArrayList.java:15)
import java.util.ArrayList;
import java.util.Iterator;
public class TestArrayList {
public static void main(String[] args) {
ArrayList al=new ArrayList();
al.add("java01");
al.add("java02");
al.add("java03");
Iterator itr=al.iterator();
while(itr.hasNext())
{
String a=(String)itr.next();
if(!al.contains("java01"))
{
al.add("asd");
System.out.println("ni");
}
}
}
}
为什么行 |
|