再仔细看看你的代码,当然会有ConcurrentModificationException了.
你看看你把迭代Iterator放在什么位置的?你先对ArrayList迭代,再往ArrayList内add.把Iterator的获取放到while循环的前面. BTW: for循环优于while循环.
for(Iterator it=c.iterator(); it.hasNext(); ) {}优于
Iterator it = c.iterator();
while ( it.hasNext() ) {}
你看看你把迭代Iterator放在什么位置的?你先对ArrayList迭代,再往ArrayList内add.把Iterator的获取放到while循环的前面. BTW: for循环优于while循环.
for(Iterator it=c.iterator(); it.hasNext(); ) {}优于
Iterator it = c.iterator();
while ( it.hasNext() ) {}
for ( int i = 1; i < 10 ; i++)
c.add(new Integer(i));
Iterator it = c.iterator();
int[] is = new int[c.size()];
int i = 0;
while (it.hasNext()) {
Integer I = (Integer)it.next();
is[i] = I.intValue();
i++;
}
你在做Iterator it = c.iterator()的时候c里面什么东西没有.it 得到的是空的Iterator.当然会出错,将Iterator it = c.iterator();这句话放在你的for 循环后面就行了
声明后,在遍历之前不要再改变 c 的值,不然会ConcurrentModificationException