ConcurrentModificationException和下标(游标)异位
来源:互联网 发布:90后网络女歌手有哪些 编辑:程序博客网 时间:2024/05/29 02:47
public class Test { private static List<String> list = new ArrayList<String>(); public static void init(int num) { for (int i = 0; i < num; i++) { list.add(i + ""); } } public static void main(String[] args) { int num = 5; init(num); for (String string : list) { //迭代一样修改异常。正常循环不会并发修改异常 if (string.equals(num - 1 + "")) //num-2时引发游标异位 { System.out.println("执行remove操作"); list.remove(string); } else { System.out.println(string); } } } }
输出
0
1
2
3
执行remove操作
Exception in thread “main” java.util.ConcurrentModificationException
修改为
public static void main(String[] args) { int num = 5; init(num); List<String> delList = new ArrayList<String>();//添加临时list集合 for (String string : list) { // if (string.equals(num - 1 + "")) if (string.equals(num - 2 + "")) { System.out.println("执行remove操作"); //不能这样一个一个移除啦 list.remove(string); delList.add(string); } else { System.out.println(string); } } list.removeAll(delList);// 统一移除 } }
阅读全文
0 0
- ConcurrentModificationException和下标(游标)异位
- (c语言)指针和数组下标
- 重写bitset表达式,使用下标操作符对测验结果进行置位(置1)和复位(置0)
- 游标、游标的优点和种类(这是转的)
- 异常--ConcurrentModificationException(53)
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- ConcurrentModificationException
- 一个实例告诉你:Kaggle 数据竞赛都有哪些套路
- Spring+Redis session共享
- 2.运行 shell 脚本
- wakeonlan-远程开机
- python2.7基于selenium的web自动化测试项目--finance
- ConcurrentModificationException和下标(游标)异位
- 弧形菜单2(动画渐入)
- Filebeat日志收集器 安装和配置
- 【Apache Kylin 】大数据下的OLAP解决方案(作业构建)
- swift 随机产生颜色
- 使用GPUImage 的lookup table为图片添加滤镜
- 7设计模式 之 观察者 和监听器的区别
- 计算机网络基础知识总结
- 一些英文单词的标准简写