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中的元素(源码分析)
ArrayList中的modCount与ConcurrentModificationException的更多相关文章
- ArrayList中ConcurrentModificationException
java中两种基本的集合结构ArrayList和LinkedList底层有两种不同的存储方式实现,ArrayList为数组实现,属于顺序存储,LinkedList为链表实现,属于链式存储,在对Arra ...
- ArrayList中modCount的作用
在ArrayList中有个成员变量modCount,继承于AbstractList. 这个成员变量记录着集合的修改次数,也就每次add或者remove它的值都会加1.这到底有什么用呢? 先看下面一段测 ...
- 为什么阿里巴巴要求谨慎使用ArrayList中的subList方法
GitHub 3.7k Star 的Java工程师成神之路 ,不来了解一下吗? GitHub 3.7k Star 的Java工程师成神之路 ,真的不来了解一下吗? GitHub 3.7k Star 的 ...
- 谨慎使用ArrayList中的subList方法
转自:https://www.toutiao.com/a6705958780460335619/?tt_from=weixin&utm_campaign=client_share&wx ...
- 迭代器使用过程中为什么抛出ConcurrentModificationException
出现的场景:在迭代器对集合进行遍历的同时,集合本身进行变更操作(add(), remove(), set()). 当正常调用时: import java.util.ArrayList; import ...
- 从`ArrayList`中了解Java的迭代器
目录 什么是迭代器 迭代器的设计意义 ArrayList对迭代器的实现 增强for循环和迭代器 参考链接 什么是迭代器 Java中的迭代器--Iterator是一个位于java.util包下的接口,这 ...
- ArrayList中的一些小细节@JDK8
ArrayList中的一些小细节@JDK8 protected transient int modCount = 0; 该变量用于记录ArrayList的版本号,不可被序列化,每次对ArrayList ...
- ArrayList中元素去重问题
如题所示,如果一个ArrayList中包含多个重复元素,该如何去重呢? 思路一以及实现: 声明2个ArrayList,分别为listA与listB ,listA为待去重list ,listB 保存去重 ...
- Java 删除ArrayList中重复元素,保持顺序
// 删除ArrayList中重复元素,保持顺序 public static List<Map<String, Object>> removeDuplicat ...
随机推荐
- Checkbutton
#tkinter之Checkbutton篇 #Checkbutton又称为多选按钮,可以表示两种状态,On和Off,可以设置回调函数,每当点击此按钮时回调函数被调用. 1.一个简单的Checkbutt ...
- SQL基础-----DML语句
之前已经介绍过SQL基础之DDL(数据库定义语言)语句,http://www.cnblogs.com/cxq0017/p/6433938.html(这是地址) 这篇文章主要介绍DML语句(数据库操纵语 ...
- 使用REST风格完成MVC前后端分离
一个具有REST风格项目的基本特征: 具有统一响应结构 前后台数据流转机制(HTTP消息与Java对象的互相转化机制) 统一的异常处理机制 参数验证机制 Cors跨域请求机制 鉴权机制 一:统一响应结 ...
- 【微信小程序】对微信http请求API的封装,方便对错误码进行处理
/** * App 微信配置文件app.js * author: nujey * versions: 1.0.0 */ App({ /** * @param {Object ...
- mysql优化2:列类型选择原则
1.字段类型优先级 整型>date,time>enum,char>varchar>blog,text 列的特点分析: 整型:定长,没有国家/地区之分,没有字符集的差异 比如ti ...
- [ZJOI 2015]幻想乡战略游戏
Description 傲娇少女幽香正在玩一个非常有趣的战略类游戏,本来这个游戏的地图其实还不算太大,幽香还能管得过来,但是不知道为什么现在的网游厂商把游戏的地图越做越大,以至于幽香一眼根本看不过来, ...
- [PA 2014]Kuglarz
Description 魔术师的桌子上有n个杯子排成一行,编号为1,2,…,n,其中某些杯子底下藏有一个小球,如果你准确地猜出是哪些杯子,你就可以获得奖品.花费c_ij元,魔术师就会告诉你杯子i,i+ ...
- 素数密度_NOI导刊2011提高(04)
题目描述 给定区间[L, R](L <= R <= 2147483647,R-L <= 1000000),请计算区间中素数的个数. 输入 两个数 L 和 R. 输出 一行,区间中素数 ...
- 使用PL/SQL Developer连接远程DB(本机不安装Oracle客户端)
本文内容亲测可行环境: 服务端:centos6.7 ,oracle 11g r2 ,动态注册,监听端口号:1521 用户端:win7 ,没有安装ORACLE及其客户端 准备文件:plsql ...
- Java Web -【分页功能】详解
分页简介 分页功能在网页中是非常常见的一个功能,其作用也就是将数据分割成多个页面来进行显示. 使用场景: 当取到的数据量达到一定的时候,就需要使用分页来进行数据分割. 当我们不使用分页功能的时候,会面 ...