Java中List迭代过程中删除、新增元素的处理
2015-09-09 09:32
369 查看
异常信息:
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at myDemo.MyTest.testIterator(MyTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
代码:
Iterator指向的原始数据发生变化时,Iterator自己就迷失了方向。
解决办法:调用其接口即Irerator的remove方法。
参考资料:http://www.cnblogs.com/dolphin0520/p/3933551.html
java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:859)
at java.util.ArrayList$Itr.next(ArrayList.java:831)
at myDemo.MyTest.testIterator(MyTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
代码:
@Test public void testIterator (){ //测试ArrayList迭代过程中删除元素, //避免抛出 java.util.ConcurrentModificationException List<User> list = new ArrayList<User>(); for(int i = 0 ; i < 10 ; i ++){ list.add(new User(i+"")); } for(User temp:list){ if(Integer.parseInt(temp.name) % 2 == 0){ list.remove(temp); //这里引起异常,这种迭代方式新增删除都会引起异常 } System.out.print(temp.name + ","); } System.out.println("====="); Iterator<User> it = list.iterator(); while(it.hasNext()){ //正确做法 //System.out.println(it.hasNext()); User temp = it.next(); if(Integer.parseInt(temp.name) % 2 == 0){ it.remove(); } } for(User temp:list){ System.out.print(temp.name + ","); } }
class User{ String name = ""; public User(String name){ this.name = name; } }
问题分析:从API中可以看到List等Collection的实现并没有同步化,如果在多线程应用程序中出现同时访问,而且出现修改操作的时候都要求外部操作同步化;调用Iterator操作获得的Iterator对象在多线程修改Set的时候也自动失效,并抛出java.util.ConcurrentModificationException。这种实现机制是fail-fast,对外部的修改并不能提供任何保证。
Iterator是工作在一个独立的线程中,并且拥有一个 mutex锁,就是说Iterator在工作的时候,是不允许被迭代的对象被改变的。Iterator被创建的时候,建立了一个内存索引表(单链表),这 个索引表指向原来的对象,当原来的对象数量改变的时候,这个索引表的内容没有同步改变,所以当索引指针往下移动的时候,便找不到要迭代的对象,于是产生错误。List、Set等是动态的,可变对象数量的数据结构,但是Iterator则是单向不可变,只能顺序读取,不能逆序操作的数据结构,当Iterator指向的原始数据发生变化时,Iterator自己就迷失了方向。
解决办法:调用其接口即Irerator的remove方法。
Iterator<User> it = list.iterator(); while(it.hasNext()){ //正确做法 //System.out.println(it.hasNext()); User temp = it.next(); if(Integer.parseInt(temp.name) % 2 == 0){ it.remove(); } }
参考资料:http://www.cnblogs.com/dolphin0520/p/3933551.html
相关文章推荐
- Java常见报错解决办法
- ubuntu修改java安全性
- Java第三周-Double与String类型变量相互转换
- Java第三周-数的比较与绝对值的计算
- java.util.ConcurrentModificationException
- 【Java EE 学习 35 下】【struts2】【struts2文件上传】【struts2自定义拦截器】【struts2手动验证】
- java jstack dump 线程 介绍 解释
- Spring学习笔记(九):Spring拦截器和java过滤器的区别
- Eclipse使用小技巧——备忘
- Spring学习笔记(八):数据库连接池的小例子
- Java第四周常用类与集合框架之接口+抽象类9-9
- java.lang.NoClassDefFoundError: javax/mail/*
- 浅谈java实现重载的方法
- Spring中@Autowired注解、@Resource注解的区别 .
- Spring中@Autowired注解、@Resource注解的区别 .
- 举例讲解Java中数组和字符串类型的使用方法
- Java Switch(String)
- 为什么JAVA只允许单继承
- javaweb -- 获取请求IP(附实现源码)
- Eclipse中jsp、js文件编辑时,卡死现象解决汇总