ArrayList中的modCount与ConcurrentModificationException

在看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中的元素(源码分析)

时间: 2024-08-17 05:59:24

ArrayList中的modCount与ConcurrentModificationException的相关文章

ArrayList中modCount的作用

在ArrayList中有个成员变量modCount,继承于AbstractList. 这个成员变量记录着集合的修改次数,也就每次add或者remove它的值都会加1.这到底有什么用呢? 先看下面一段测试代码: package temp; import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class demo { public static void main(String[]

ArrayList中Iterator的研究学习

最近去某公司面试,被问到了一个简单的问题,ArrayList中要删除一些元素应该怎么操作?答曰:"使用Iterator迭代器遍历,判断之后再用迭代器提供的remove()方法将判断为true的元素删掉",问:“为什么要选择这个方法?”答曰:“迭代器采用的是fail-fast机制,foreach循环内部调用了迭代器,删除元素破坏了集合的结构,所以会报错”,追问:“为什么不能用for循环呢?没使用迭代器也应该可行啊?为什么迭代器又是可以的呢?” 答曰:“因为for循环删掉元素后会错位,至于

谨慎使用ArrayList中的subList方法

转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wxshare_count=1&timestamp=1561423940&app=news_article&utm_source=weixin&utm_medium=toutiao_android&req_id=201906250852200101520492020364F

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

出现的场景:在迭代器对集合进行遍历的同时,集合本身进行变更操作(add(), remove(), set()). 当正常调用时: import java.util.ArrayList; import java.util.Iterator; public class TestDemo { public static void main(String[] args) { ArrayList<Integer> a = new ArrayList<>(); a.add(1); a.add(

去除ArrayList中的重复元素

ArrayList中可以存在重复元素的,若要去除重复元素必须要进行扫描,其实在原理上和数组去除重复元素是一样的. 可以利用contains方法来确定ArrayList中是否存在某个元素. 但是ArrayList中可以放任意的对象,那怎么定义各个对象是否是相同的? 可以通过自己定义类的专属equals方法,定义规则确定某两个对象是否相同,因为contains在判断某个元素是否存在ArrayList中,也是在比较这个元素时候是否和容器中存在的元素相同,若相同则存在,若不相同则不存在,contains

如何把一个Array 复制到ArrayList中?

如何把一个Array 复制到ArrayList中? string[] array = new string[]{" 1", "2"," 3", "4"," 5" }; ArrayList list = new ArrayList();一:使用for循环,将array数组中的数据逐步加入到ArrayList的对象中: //1.for循环            for (int i = 0; i < ar

ArrayList中remove()方法删除长度大于5的元素之后下标重定位的问题

1.问题阐述 需求: 有一个ArrayList数组,要求删除长度大于5的字符串,如:arr = {"ab1","123ad","bca","dadfadf","dddaaa","你好啊","我来啦","别跑啊"}: 要求结果输出: {"ab1","123ad","bca","你好

Java之——删除ArrayList中的反复元素的2种方法

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47414935 ArrayList是Java中最经常使用的集合类型之中的一个.它同意灵活加入多个null元素,反复的元素,并保持元素的插入顺序.在编码时我们经常会遇 到那种必须从已建成的ArrayList中删除反复元素的要求.这篇文章将给出两种从ArrayList中删除反复元素的方法. 方法1:使用HashSet删除ArrayList中反复的元素 在该方法中.我们使用HashSet

ArrayList中重复元素处理方法.[Java]

1.使用HashSet删除ArrayList中重复的元素 private static void sortByHashSet() { ArrayList<String> listWithDuplicateElements = new ArrayList<String>(); listWithDuplicateElements.add("JAVA"); listWithDuplicateElements.add("J2EE"); listWit