Java集合遍历时进行remove报错

以ArrayList举例讲解,对于遍历list集合,一般有两种方式,第一种直接用数组下标标遍历,第二种用forEach遍历,也就是用ArrayList的内部类Itr遍历,该内部实现了Iterator接口,也就是常说的迭代器,无论是哪种方式,都不要在遍历过程中用list自身的remove方法进行操作,第二种方式可能会抛出java.util.ConcurrentModificationException

在这里插入图片描述

第一种遍历

List<String> strList = new ArrayList<>();
	strList.add("a");//0
	strList.add("b");//1
	strList.add("c");//0
	strList.add("d");//1
	strList.add("e");//0
	strList.add("f");//1
	for(int i = 0; i < strList.size(); i++) {
		if (i % 2 == 0) {
			strList.remove(i);
	}
}

这种方式最常用,虽然不会抛异常,但是当删除角标为 i 的元素时,前一个角标 i+1 位置的元素会顺移到 i 的位置,list的size也会-1,本来是要删除a c e,最终删的却是 a d,这种情况如果把for循环条件直接写成 i<5 ,则当i=4时会发 IndexOutOfBoundsException,如果要删除元素,建议用迭代器遍历,然后用迭代器的remove方法删除。

第二种遍历

for (int i=0; i<list.size(); i++) {
//	if ("a".equals(s))
	if ("b".equals(s))
		list.remove(s);
}

在这里插入图片描述
这种方式当remove a时是可以正常删除不会报错的,但是如果是先remove b则会报ConcurrentModificationException
当用forEach遍历时,会调用迭代器的 hasNext 方法,cursor代表当前要操作的角标,size代表集合元素个数,modCount 记录对集合的修改次数,初始值为0,由于往集合里放了a b,则此时modCount为2,expectedModCount 为期望的修改其次数,理论上得和modCount相同,在开始remove元素前,expectedModCountmodCount相同,为2。

/**
     * The size of the ArrayList (the number of elements it contains).
     *
     * @serial
     */
private int size;
int cursor;       // index of next element to return
int expectedModCount = modCount;

public boolean hasNext() {
	return cursor != size;
}

分析一下,如果一开始remove b,此时cursor 为1 size为2 满足 cursor != size,则hasNext返回true,会调用next方法,next方法里面会先调用checkForComodification方法,该方法用来检测modCountexpectedModCount是否相等,如果不相等则会抛出ConcurrentModificationException,此时咱们还没有删除任何元素,则此时不会抛异常,next方法顺利放回b,然后调用list的remove方法删除b,顺利删除,并且该方法第二行 modCount++ 先把修改次数 +1,而且没有对 expectedModCount 做任何修改;删完b后进行下次遍历,依旧是上面的步骤,此时应该cursor为2,size为1,hasNext返回true,然后checkForComodification方法里面发现 modCount != expectedModCount,此时就会抛出异常。
以同样的方式分析下先remove a 的情况,cursor为0 size为2, 不相等,modCount为2 expectedModCount为2,相等,此时顺利删除a,然后第二次遍历时,cursor为1 size为1,不相等,方法返回,不会报错。

@SuppressWarnings("unchecked")
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];
}

final void checkForComodification() {
	if (modCount != expectedModCount)
    	throw new ConcurrentModificationException();
}

public E remove(int index) {
	rangeCheck(index);

	modCount++;
	E oldValue = elementData(index);

	int numMoved = size - index - 1;
	if (numMoved > 0)
		System.arraycopy(elementData, index+1, elementData, index, numMoved);
 	elementData[--size] = null; // clear to let GC do its work

	return oldValue;
}

那么,如果用迭代器的remove方法为什么就不会出现ConcurrentModificationException呢,看源码便一目了然,关键代码 expectedModCount = modCount,每次修改都会重新给 expectedModCount 赋值,自然不会出现ConcurrentModificationException错误,这个很好理解,在遍历集合时,如果你对集合做了增删操作而且没有告诉我,那可能会有意想不到的错误,看到一个不太恰当的比喻,老师在点名时,如果有同学出去了,那点名就没办法顺利点完,这种措施其实就是fastFail机制,一定程度用来预防多线程操作集合可能会遇到的错误,但是多线程操作集合还是推荐加锁或者用juc包下面的线程安全集合工具。

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方法删除元素,个人记录,如有错误,请友善支出,谢谢。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值