Java ConcurrentModificationException异常原因和单线程下的解决方法

今天写一个商品购物车信息管理的小项目,在删除信息时出现了ConcurrentModificationException异常,图如下:
在这里插入图片描述
出现异常的方法如下:
在这里插入图片描述
通过查阅资料发现,ArrayList在迭代的时候如果同时对其进行修改就会抛出java.util.ConcurrentModificationException异常;
下面是ArrayList类中出错信息对应的类

`
    /**
     * An optimized version of AbstractList.Itr
     */
    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();
        }
    }

通过源码发现在ArrayList的源码中的iterator()这个方法是实现的接口中的方法得来的,接着看源码,调可以发现,出错的关键是用list.remove()方法导致modCount和expectedModCount的值不一致,所以报错。
通过查看源码我们可以发现,当前方法提供了一个remove()方法,所以我们可以直接使用该方法,不同的是需要多一步迭代器的声明,修改后的代码如下:

public void delectShopCarByName(String name){
        //System.out.println("进入删除方法");
        Iterator iterator = shopCarlist.iterator();
//        int i =1;
        while(iterator.hasNext()) {
//            System.out.println("进入删除方法的for循环"+i++);
            ShopCar shopCar = (ShopCar) iterator.next();
            if(shopCar.getItems().getName().equals(name)){
                iterator.remove();
            }
        }

    }

运行发现,问题解决。
资料查看网址为

https://www.cnblogs.com/zhuyeshen/p/10956822.html

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值