List可以边遍历边删除吗?

当问到List几个一边遍历一边删除时我想第一时间就会写出以下的代码


/**
 * @Author:cbx
 * @Date:2020/10/16/13:12
 */
public class TestList {
    public static void main(String[] args) {
        List<String> resList=new ArrayList<>();
        resList.add("james");
        resList.add("kd");
        resList.add("kobe");
        for (String value:resList){
            if (value.equals("kd")){
                resList.remove(value);
            }
        }
        System.out.println(resList);
    }
}

结果却是爆了一个java.util.ConcurrentModificationException(并发修改异常)
在这里插入图片描述
查看一下刚才代码的字节码

public class TestList {
    public TestList() {
    }

    public static void main(String[] args) {
        List<String> resList = new ArrayList();
        resList.add("james");
        resList.add("kd");
        resList.add("kobe");
        Iterator var2 = resList.iterator();

        while(var2.hasNext()) {
            String value = (String)var2.next();
            if (value.equals("kd")) {
                resList.remove(value);
            }
        }

        System.out.println(resList);
    }
}

forEach循环在实际执行时,其实使用的是Iterator,使用的核心方法是hasNext()和next()。

看一下ArrayList类的Iterator是如何实现的?

   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;

        Itr() {}

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

        @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];
        }

        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();
            }
        }

        @Override
        @SuppressWarnings("unchecked")
        public void forEachRemaining(Consumer<? super E> consumer) {
            Objects.requireNonNull(consumer);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i >= size) {
                return;
            }
            final Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length) {
                throw new ConcurrentModificationException();
            }
            while (i != size && modCount == expectedModCount) {
                consumer.accept((E) elementData[i++]);
            }
            // update once at end of iteration to reduce heap write traffic
            cursor = i;
            lastRet = i - 1;
            checkForComodification();
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
            //这里就是具体报错的地方,抛出了一个并发修改异常
                throw new ConcurrentModificationException();
        }
    }

这里需要比较modCount和expectedModCount的值
刚开始modCount和expectedModCount都是3

执行了remove方法后
在这里插入图片描述
所以下一次再进行调用next()方法的时候,校验时就会出现modCount!=expectedModCount 而抛出异常。

解决方法

  1. 使用Iterator的remove()方法
  2. 使用for循环正序遍历
  3. 使用for循环倒序遍历

使用iterator的remove方法

在这里插入图片描述

使用for循环正序遍历

 for(int i=0;i<resList.size();i++){
             String item=resList.get(i);
             if (item.equals("kd")){
                 resList.remove(i);
                 //注意,必须这里要索引-1,因为数组该位置被删除后,后面的替上来
                 i=i-1;
             }
        }

使用for循环逆序遍历

  for(int i=resList.size();i>=0;i--){
            String item=resList.get(i);
            if (item.equals("kd")){
                resList.remove(i);
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值