在看ArrayList源码时,看到了一个字段modCount。在add、remove、clear等方法中都有modCount++的操作。不明白什么意思。点进去看了看该字段的解释,总算明白了。modCount是在AbstractList抽象类中定义的。该字段的解释如下所示。

    /**
     * The number of times this list has been <i>structurally modified</i>.
     * Structural modifications are those that change the size of the
     * list, or otherwise perturb it in such a fashion that iterations in
     * progress may yield incorrect results.
     * 该list结构性改变的次数。结构改变是指list的大小改变了,或者顺序被打乱,从而
     * 导致iterations在执行过程中得到错误的结果。
     *
     * <p>This field is used by the iterator and list iterator implementation
     * returned by the {@code iterator} and {@code listIterator} methods.
     * If the value of this field changes unexpectedly, the iterator (or list
     * iterator) will throw a {@code ConcurrentModificationException} in
     * response to the {@code next}, {@code remove}, {@code previous},
     * {@code set} or {@code add} operations.  This provides
     * <i>fail-fast</i> behavior, rather than non-deterministic behavior in
     * the face of concurrent modification during iteration.
     * 该字段用于iterator和listIterator方法返回的iterator和list iterator实现数据迭代。
     * 如果该字段出现了意外的改变,iterator/list iterator在执行next、remove、previous、
     * set、add等操作时将抛出ConcurrentModificationException。 这样就提供了快速失败行为,
     * 而不会出现在执行迭代过程中的不确定行为。
     *
     * <p><b>Use of this field by subclasses is optional.</b> If a subclass
     * wishes to provide fail-fast iterators (and list iterators), then it
     * merely has to increment this field in its {@code add(int, E)} and
     * {@code remove(int)} methods (and any other methods that it overrides
     * that result in structural modifications to the list).  A single call to
     * {@code add(int, E)} or {@code remove(int)} must add no more than
     * one to this field, or the iterators (and list iterators) will throw
     * bogus {@code ConcurrentModificationExceptions}.  If an implementation
     * does not wish to provide fail-fast iterators, this field may be
     * ignored.
     * 子类是否使用该字段是可选的。 如果子类想要提供快速失败的iterator/list iterator,
     * 只需要在其add、remove或者其他重载的会导致list结构改变的方法中增加该字段的值。
     * 单次都add或者remove的调用对该字段值的增加不能超过1,否则iterator/list iterator
     * 将抛出虚假的ConcurrentModificationExceptions。 如果实现类不想提供快速失败的迭代器,
     * 可以忽略掉该字段。
     */
    protected transient int modCount = 0;
    //如果不晓得transient干嘛的,请自觉去面壁☺

关于ConcurrentModificationException,请参考:Java遍历时删除List、Set、Map中的元素(源码分析)

ArrayList中的modCount与ConcurrentModificationException的更多相关文章

  1. ArrayList中ConcurrentModificationException

    java中两种基本的集合结构ArrayList和LinkedList底层有两种不同的存储方式实现,ArrayList为数组实现,属于顺序存储,LinkedList为链表实现,属于链式存储,在对Arra ...

  2. ArrayList中modCount的作用

    在ArrayList中有个成员变量modCount,继承于AbstractList. 这个成员变量记录着集合的修改次数,也就每次add或者remove它的值都会加1.这到底有什么用呢? 先看下面一段测 ...

  3. 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法

    GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...

  4. 谨慎使用ArrayList中的subList方法

    转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...

  5. 迭代器使用过程中为什么抛出ConcurrentModificationException

    出现的场景:在迭代器对集合进行遍历的同时,集合本身进行变更操作(add(), remove(), set()). 当正常调用时: import java.util.ArrayList; import ...

  6. 从`ArrayList`中了解Java的迭代器

    目录 什么是迭代器 迭代器的设计意义 ArrayList对迭代器的实现 增强for循环和迭代器 参考链接 什么是迭代器 Java中的迭代器--Iterator是一个位于java.util包下的接口,这 ...

  7. ArrayList中的一些小细节@JDK8

    ArrayList中的一些小细节@JDK8 protected transient int modCount = 0; 该变量用于记录ArrayList的版本号,不可被序列化,每次对ArrayList ...

  8. ArrayList中元素去重问题

    如题所示,如果一个ArrayList中包含多个重复元素,该如何去重呢? 思路一以及实现: 声明2个ArrayList,分别为listA与listB ,listA为待去重list ,listB 保存去重 ...

  9. Java 删除ArrayList中重复元素,保持顺序

    // 删除ArrayList中重复元素,保持顺序          public static List<Map<String, Object>> removeDuplicat ...

随机推荐

  1. enumerate给列表加索引

    >>> list = ['a','b','c'] >>> for i,j in enumerate(list): print(i,j) 0 a 1 b 2 c &g ...

  2. Flask博客开发——登录验证码

    这部分为Flask博客的登录页面加个验证码.使用了PIL模块生成验证码图片,并通过Flask的session机制,进行验证码验证. 1.生成验证码 使用string模块:string.ascii_le ...

  3. C#之Excel操作

    下面的这几个方法是我在项目中经常用到的,欢迎大家批评指正 读取Excel表中的数据 第一种:功能丰富,速度慢 /// <summary> /// 从Excel读取数据 /// </s ...

  4. APP开发外包时常见的错误

    时代在发展,科技在进步,很多企业都想要开发出属于自己的一款APP.然而,不是每个企业都有开发团队或是专门雇一个技术人员来做的,有一个好方法就是把开发APP的工作外包出去,找一个比较好的外包公司根据自己 ...

  5. [LeetCode] My Calendar I 我的日历之一

    Implement a MyCalendar class to store your events. A new event can be added if adding the event will ...

  6. javascript实现双向数据绑定

    双向数据绑定已经是面试中经常被问到的点,需要对原理和实现都要有一定了解. 下面是实现双向绑定的两种方法: 属性劫持 脏数据检查 一.属性劫持 主要是通过Object对象的defineProperty方 ...

  7. 分析 ajax 请求并抓取今日头条街拍美图

    首先分析街拍图集的网页请求头部: 在 preview 选项卡我们可以找到 json 文件,分析 data 选项,找到我们要找到的图集地址 article_url: 选中其中一张图片,分析 json 请 ...

  8. [Luogu 2265]路边的水沟

    Description LYQ市有一个巨大的水沟网络,可以近似看成一个n*m的矩形网格,网格的每个格点都安装了闸门,我们将从水沟网络右下角的闸门到左上角的闸门的一条路径称为水流. 现给定水沟网的长和宽 ...

  9. hihocoder 1388 fft循环矩阵

    #1388 : Periodic Signal 时间限制:5000ms 单点时限:5000ms 内存限制:256MB 描述 Profess X is an expert in signal proce ...

  10. POJ 1324(BFS + 状态压缩)

    题意:给你一条蛇,要求一以最少的步数走到1,1 思路: 最开始一直没想到应该怎样保存状态,后来发现别人用二进制保存蛇的状态,即每两个节点之间的方向和头节点,二进制最多14位(感觉状态保存都能扯到二进制 ...