在看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. 原生js中实现全选和反选功能

    <!DOCTYPE html>      <html>      <head lang="en">          <meta char ...

  2. 关于阮大神的es6标准入门第一章

    题记:之前在10月份的时候写过阮大神的es6的第一章,但是由于那段时间项目组的动荡,所以也没有什么后续,导致我现在对es6基本都忘的差不多了,不过,现在换了新公司,最近也没什么任务,所以现在开始重新写 ...

  3. 网络安全实验室_上传关writeup

    请上传一张jpg格式的图片 先传个图片码试试 我肯定乖嘛(#`Д´)ノ 气到改后缀 请上传一张jpg格式的图片 我猜是00截断,不信来试试 先在赋值1.php .jpg,接着去hex中找到空格改成00 ...

  4. [FJOI2007]轮状病毒

    题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间的信息通道,如图1. n轮状病毒的产生规律 ...

  5. [HNOI2011]数学作业

    题目描述 小 C 数学成绩优异,于是老师给小 C 留了一道非常难的数学作业题: 给定正整数 N 和 M,要求计算 Concatenate (1 .. N) Mod M 的值,其中 Concatenat ...

  6. 【USACO】干草金字塔

    题目描述 贝西要用干草包堆出一座金字塔.干草包会从传送带上陆续运来,依次出现 N 包,每包干草可 以看做是一个二维平面上的一个长方形,第 i 包干草的宽度是 W i ,长度统一为 1. 金字塔的修建有 ...

  7. hdu 5130(2014广州 圆与多边形相交模板)

    题意:一个很多个点p构成的多边形,pb <= pa * k时p所占区域与多边形相交面积 设p(x,y),       (x - xb)^2+(y - yb)^2 / (x - xa)^2+(y ...

  8. [BZOJ]1089 严格n元树(SCOI2003)

    十几年前的题啊……果然还处于高精度遍地走的年代.不过通过这道题,小C想mark一下n叉树计数的做法. Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该 ...

  9. 容器化分布式日志组件ExceptionLess的Angular前端UI

    写在前面 随着微服务架构的流行,日志也需要由专门的分布式日志组件来完成这个工作,我们项目使用的是 ExceptionLess 这个组件,它是前后端分离的:这篇文章我们就来实践容器化 Exception ...

  10. thymeltesys-基于Spring Boot Oauth2的扫码登录框架

    thymeltesys thymelte是一个基于Spring Boot Oauth2的扫码登录框架,使用PostgreSQL存储数据,之后会慢慢支持其他关系型数据库.即使你不使用整个框架,只使用其中 ...