ListIterator使用时的ConcurrentModificationException异常问题
2017-07-29 17:40
316 查看
写作业时用到了迭代器ListIterator,主要代码如下,使用了
报错Exception in thread "main" java.util.ConcurrentModificationException,检查后发现,是下面这句代码的使用使程序抛出了异常
workerList.add(index, new Worker("zhao6",24,3300));
ListIterator it 和集合 workList 同时操作一个资源,语句下方还有
-----------------------------------------------------------------------------------------------------
注意:使用ListIterator的
遍历集合时,注意可能因指针不在集合的头或尾而"错过"某些数据
.hasNext().next()
.hasPrevious()
.previous()
ListIterator<Worker> it = workerList.listIterator(); int index = 0; while(it.hasNext()) { Worker w = it.next() ; if("li4".equals(w.getName())) { // it.add( new Worker("zhao6",24,3300)); index = workerList.indexOf(w); System.out.println(index); // break; } } workerList.add(index, new Worker("zhao6",24,3300)); System.out.println(workerList.toString()); // ListIterator<Worker> it1 = workerList.listIterator(); while(it.hasPrevious()) { Worker w = it.previous() ; if("zhang3".equals(w.getName())) { it.remove(); // break; } System.out.println(workerList.toString()); }
报错Exception in thread "main" java.util.ConcurrentModificationException,检查后发现,是下面这句代码的使用使程序抛出了异常
workerList.add(index, new Worker("zhao6",24,3300));
ListIterator it 和集合 workList 同时操作一个资源,语句下方还有
while(it.hasPrevious())即ListIterator it 未完成对集合的操作,此时就会抛出异常
-----------------------------------------------------------------------------------------------------
注意:使用ListIterator的
.hasNext().next()
.hasPrevious()
.previous()
遍历集合时,注意可能因指针不在集合的头或尾而"错过"某些数据
相关文章推荐
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- LinkedBlockingQueue操作,线程安全问题,ConcurrentModificationException 异常分析与解决方案
- 使用迭代器遍历集合时,当集合中的数据发生变化是会抛出java.util.ConcurrentModificationException异常
- java.util.ConcurrentModificationException 解决办法(使用迭代器时出现异常)
- Java Iterator ListIterator 迭代器 以及 并发修改异常 ConcurrentModificationException
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常的解决办法
- 使用set时发生Java ConcurrentModificationException异常原因和解决方法
- 关于对Map、List等集合操作抛出ConcurrentModificationException 异常问题
- 遍历变量导致ConcurrentModificationException异常的问题
- 关于出现List的java.util.ConcurrentModificationException异常问题的解决
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- Java ConcurrentModificationException异常问题
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- 使用迭代器遍历List的时候修改List报ConcurrentModificationException异常原因分析
- java.util.ConcurrentModificationException异常的解决办法
- java迭代器异常 java.util.ConcurrentModificationException at java.util.HashMap$HashIterator.nextEntry
- ConcurrentModificationException 的异常分析
- java.util.ConcurrentModificationException 异常
- java.util.ConcurrentModificationException异常 解决方法
- 从ArrayList中删除对象报java.util.ConcurrentModificationException异常解决