前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ArrayList为什么要自己实现迭代器

ArrayList为什么要自己实现迭代器

作者头像
逝兮诚
发布2019-10-30 18:28:24
7990
发布2019-10-30 18:28:24
举报
文章被收录于专栏:代码人生代码人生

ArrayList的父类AbstractList已经实现了Iterator接口,为什么ArrayList还要自己实现Iterator接口呢?

ArrayList实现Iterator,是为了避免ArrayList在迭代过程中数组结构发生变化的而产生的问题,这个处理机制称为Fail-Fast机制,实际是一个乐观锁,实现如下。

ArrayList有一个modCount属性,在add(),remove()执行开始,modCount++。ArrayList创建迭代器对象时,会复制当前modCount到expectedModCount,迭代器每次执行next(),remove(),forEachRemaining()时,都判断modCount是否与expectedModCount相同,若不相同,则抛出异常。

代码语言:javascript
复制
    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 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();
        }    
    }

ArrayList不是线程安全的,Fail-Fast不是来解决多线程的问题的。这个的实际意义在于单线程时,新手在foreach中做add(),remove()操作。如果要解决多线程,要在add()和remove()中加上modCount的效验了。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档