为什么ArrayList remove报错
不报错
List<String> userNames = new ArrayList<String>() {{
add("Hollis");
add("hollis");
add("HollisChuang");
add("H");
}};
for (int i = 0; i < userNames.size(); i++) { // 每一次循环都会重新计算当前的size, 从而i不会越界
if (userNames.get(i).equals("Hollis")) {
userNames.remove(i);
}
}
System.out.println(userNames);
会报错 ConcurrentModificationException
List<String> userNames = new ArrayList<String>() {{
add("Hollis");
add("hollis");
add("HollisChuang");
add("H");
}};
for (String userName : userNames) {
if (userName.equals("Hollis")) {
userNames.remove(userName);
}
}
System.out.println(userNames);

通过查看源码发现,引起异常是因为modCount和expectModCount不一致导致的
final void checkForComodification() {
// modCount 和 expectedModCount 不同导致的
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
通过remove方法的源码发现 remove 调用 fastremove, fastremove中只是修改了modCount, 造成了两者的不一致.
private void fastRemove(int index) {
// 只是修改了modCount
modCount++;
// omit code
elementData[--size] = null; // clear to let GC do its work
}
为什么ArrayList remove报错的更多相关文章
- java.util.Arrays$ArrayList addAll报错
执行下面代码时报错: List<String> centerList = WebConstants.SUPPORT_BIG_CENTERS_LIST; // WebConstants.SU ...
- [Java基础] Java中List.remove报错UnsupportedOperationException
Java中List.remove(removeRange,clear类似) 报出 UnsupportedOperationException 的错误.原来该List是一个AbstractList,不支 ...
- easyui Datagrid查询报错Uncaught TypeError:Cannot read property 'length' of undefined
1.问题描述 easyui中datagrid执行loadData方法出现如下异常:Cannot read property 'length' of undefined 2.一开始怀疑是js或者页面的问 ...
- ArrayList在foreach正常迭代删除不报错的原因
一.背景 在以前的随笔中说道过ArrayList的foreach迭代删除的问题:ArrayList迭代过程删除问题 按照以前的说法,在ArrayList中通过foreach迭代删除会抛异常:java. ...
- 关于java中ArrayList的快速失败机制的漏洞——使用迭代器循环时删除倒数第二个元素不会报错
一.问题描述 话不多说,先上代码: public static void main(String[] args) throws InterruptedException { List<Strin ...
- 【java】在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException
场景: 在分页查询结果中对最后的结果集List进行操作add()或remove()操作,报错:java.lang.UnsupportedOperationException 错误: java.lang ...
- remove ---会报错discard不会报错
s = {1,2,3,4,5,6,'sn','7'} s.remove('hellfjsdjfsjdfljsdl')#删除元素不纯在会报错 print(s) s.discard("sbbbb ...
- 安装apr-1.6.3报错[cannot remove `libtoolT’: No such file or directory]解决方法
发现有这个提示:cannot remove `libtoolT’: No such file or directory , 编辑 configure文件,查找 $RM "$cfgfile&q ...
- java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page pagehelper报错无法类型转化。
报错信息: 严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request ...
随机推荐
- CCD与CMOS的区别
我们在购买相机或是摄像机时,都会看到使用CMOS镜头或是CCD镜头,那么CCD与CMOS是什么意思呢,CCD与CMOS的区别是什么?首先,让我们了解CCD与CMOS的意思. CCDCCD使用一种高感光 ...
- form:select form:options 标签数据回显
在jsp页面中经常会使用到 form:select form:options 遍历后台List集合生成 select 下拉选择器,但是 form:options 标签并没有提供一个可以回显数据的属性. ...
- bzoj 1430: 小猴打架
1430: 小猴打架 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 634 Solved: 461[Submit][Status][Discuss] ...
- JavaScript-变量与作用域链
jQuery片段: 1 var 2 // Will speed up references to window, and allows munging its name. 3 win ...
- 牛客网习题剑指offer之数值的整数次方
分析: 要考虑到exponent为0和负数的情况. 如果base是0并且exponent是负数的时候呢?那就发生除0的情况了. AC代码: public class Solution { public ...
- typeof运算符
javascript中typeof用来判断一个变量或表达式的数据类型. typeof 返回值有六种可能: "number," "string," "b ...
- UTF8字符串转换为汉字 c#
using System; /// <summary> /// UTF8字符串转换为汉字用的类 /// 转换如"\\u8d35"之类的字符串为对应的汉字 /// < ...
- Linux下文件目录权限和对应命令的总结
Linux下的权限有rwx三种,分别对应读,写,执行三种,在对文件和目录时,分别是下列含义: 对应权限的命令为: 文件: r-- cat, more, head, tail w-- echo, vi ...
- PIP安装时报The repository located at pypi.douban.com is not a trusted or secure host and is being ignore
C:\WINDOWS\system32>pip install scrapyCollecting scrapy The repository located at pypi.douban.com ...
- android 系统的休眠与唤醒+linux 系统休眠
Android休眠与唤醒驱动流程分析 标准Linux休眠过程: powermanagement notifiers are executed with PM_SUSPEND_PREPARE tasks ...