迭代器
来源:互联网 发布:淘宝网音乐芯片 编辑:程序博客网 时间:2024/06/06 02:44
ArrayList list = new ArrayList();
//对集合进行操作add 和 remove 方法
list.add(10);
//刚开始迭代器实例化时,modCount 和 expectedModCount的值是一样的
//expectedModCount=modCount=3
Iterator iterator = list.iterator();
while(iterator.hasNext()){
Integer integer = iterator.next();
//迭代器执行remove方法,此时expectedModCount=modCount=4
// iterator.remove();
//但是,如果是 集合本身执行remove方法,那么modCount=4,而expectedModCount=3 //造成判断条件为真,抛出ConcurrentModificationException异常 list.remove(0); Integer integer1 = iterator.next(); }
阅读全文