foreach循环中不能用list.remove() list.add()方法的原因分析

最近公司要进行代码重构规范,看阿里巴巴java开发手册的时候发现一个有趣的问题

List<String> list = new ArrayList<>();
list.add("1");
list.add("2");
for(String item:list){
    if("1".equals(item)){
        list.remove(item);
    }
}

当删除1的时候程序就不会报错,但是当删除2的时候就会抛出Exception in thread "main" java.util.ConcurrentModificationException,手册里要求使用的是iterator.remove()的方式,于是顺便撸一下ArrayList的源码(jdk8)。根据堆栈信息

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
    at java.util.ArrayList$Itr.next(ArrayList.java:851)
    at com.ywx.springcloud2.Springcloud2Application.main(Springcloud2Application.java:25)

报错来自ArrayList内部的Itr类的next()方法,java的for(Itrm i:list)循环底层是用iterator的hasNext和next实现,Itr是ArrayListiterator的实现(直接上代码)
private class Itr implements Iterator<E> {
    int cursor;       // index of next element to return
    int lastRet = -1; // index of last element returned; -1 if no such
    int expectedModCount = modCount;

    public boolean hasNext() {
        return cursor != size;
    }
@SuppressWarnings("unchecked")
public E next() {
    //next方法时会检查原list
    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() {
//这里比较了expectedModCount和expectedModCount这两个参数
    if (modCount != expectedModCount)
        throw new ConcurrentModificationException();
}

可以看到在expectedModCount初始化的时候是等于modCount的,然后next方法调用时每次都会检查下这两个值是否相等,(这里相当于一个乐观锁的结构)。

然后查看下这两个值都是在哪定义、改变的,expectedModCount定义位置上面已经标红,modCount 定义在ArrayList 601行,上面还有一大堆注释,表示的是这个list的size改变的次数,在list remove,add等size改变的操作中都会modCount++

/**
 * Removes the element at the specified position in this list.
 * Shifts any subsequent elements to the left (subtracts one from their
 * indices).
 *
 * @param index the index of the element to be removed
 * @return the element that was removed from the list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
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;
}

那么问题来了,同样是list.remove操作,同样改变了size,应该都报错,为什么1不报错,2报错

关键在于上面代码中标蓝色的位置,当调用next方法获取倒数第二个元素后,cusor到了最后1位大小为原始size-1,然后调用list的remove删除倒数第二个元素,size-1,下次调用hasNext时刚好返回false,所以不会调用next方法,而删除倒数第二个之前的元素时cusor<size-1删除最后一个元素cusor=size,hasNext都返回true,都会调用next方法,导致报错。

对于iterator中的remove()删除后会使expectedModCount = modCount,所以在for(Item item:list)循环中不要用remove add等会改变size的操作。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JavaforEach循环,你不能直接使用`remove`方法来删除集合的元素。这是因为`forEach`循环是基于迭代器的,而在迭代过程直接调用`remove`方法会导致`ConcurrentModificationException`并发修改异常。 如果你需要在遍历过程删除集合的元素,可以使用迭代器的`remove`方法。以下是一个示例代码,演示如何在遍历List使用迭代器的`remove`方法删除元素: ```java import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main { public static void main(String[] args) { // 创建一个List List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Orange"); // 获取List的迭代器 Iterator<String> iterator = list.iterator(); // 遍历List并删除元素 while (iterator.hasNext()) { String element = iterator.next(); if (element.equals("Banana")) { iterator.remove(); // 使用迭代器的remove方法删除元素 } } // 打印删除后的List System.out.println(list); // 输出: [Apple, Orange] } } ``` 在上述代码,我们通过调用`iterator()`方法获取List的迭代器对象。然后,在遍历过程使用`next()`方法获取下一个元素,并使用`remove()`方法删除指定的元素。注意,这里我们使用了迭代器的`remove()`方法,而不是List的`remove()`方法。 这样就可以在遍历List的过程删除元素了。希望对你有所帮助!如果你还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值