ArrayList的遍历方式问题之java.util.ConcurrentModificationException

引子:

我们来看一个问题,在对ArrayList进行遍历时,凡是涉及到使用迭代器的,都不能使用ArrayList的remove、add方法,例如下边的while循环中使用了iterator来取值,使用了集合的remove方法,报错如下:
public class demo2 {
	public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>();
        strings.add("1");
        strings.add("2");
        strings.add("3");

        Iterator<String> iterator = strings.iterator();

        while (iterator.hasNext()){
            String next = iterator.next();
            strings.remove(next);
        }
        System.out.println(strings);
	}
}

执行结果:

Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:911)
	at java.util.ArrayList$Itr.next(ArrayList.java:861)
	at demo2.main(demo2.java:17)

编译后的结果:

public class demo2 {
    public demo2() {
    }
    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList();
        strings.add("1");
        strings.add("2");
        strings.add("3");
        System.out.println(strings);
        Iterator iterator = strings.iterator();

        while(iterator.hasNext()) {
            String next = (String)iterator.next();
            strings.remove(next);
        }

        System.out.println(strings);
    }
}

原因(集合的循环):

	在ArrayList中有一个成员变量是modCount,该变量记录了Arraylist对象了操作次数,增删操作都会使modCount自增。在ArrayList
的内部,有一个迭代器的内部实现类,该类中有expectedModCount变量,其初始值为modCount在循环前的值。

1、迭代器中的next()方法

public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }
next()方法中,有一个checkForComodification()方法,在该方法中会判断modCount 和 expectedModCount是否相等,如果不等,就抛出错误
final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
所以如果在循环中使用remove\add方法,modCount就会自增,但是expectedModCount没变,就是抛出异常

2、那为什么可以使用迭代器中的remove方法?

ArrayList的迭代器内部类的remove方法:
        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
在该方法中,每次remove后,都会对expectedModCount重新赋值,所以不会出现expectedModCount != modCount的情况
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值