您的位置:首页 > 编程语言 > Java开发

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)

代码:

@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: