concurrentModificationException [英] concurrentModificationException

查看:113
本文介绍了concurrentModificationException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码段,尝试处理电子表格,需要排除临时列。我知道粗糙的方式,我做它,把异常放在一个ArrayList和处理列表上每一个,并增加在当前行列是反常的,但你知道只是得到它完成。



但是,我得到的标题错误,我相信永远不会发生。我只是循环通过ArrayList和比较,不修改任何东西。错误在哪里?是否有更好的方法来处理异常列表?

  ArrayList noProcess = new ArrayList 
迭代器itr00 = noProcess.iterator();
迭代器itr01 = noProcess.iterator();
noProcess.add(new Integer(5));
noProcess.add(new Integer(18));
....
boolean include = true;
for(int i = 0; i for(int j = 0; j< archive [i] .length; j ++){
while itr00.hasNext()){
if(j ==((Integer)itr00.next())intValue())
include = false;
}
if(include){...


解决方案

一旦你创建了一个迭代器(而不是迭代器),你不能改变 Iterable 的内容,否则你会得到一个ConcurrentModificationException当你移动迭代器 - 你创建一个迭代器,然后执行 noProcess.add(new Integer(5)); ,然后再推进迭代器。 / p>

此外,您也可以创建两个迭代器 - 您不应该这样做 - 这太疯狂了。


With the snippet below I am, attempting to process a spreadsheet, with the twist of needing to exclude ad hoc columns. I know the crude way I am doing it, put the exceptions in an ArrayList and process the list on each and ever increment over the current row columns is perverse, but you know just get it done.

However I am getting the titled error, which I believe should never happen. I am just looping through the ArrayList and comparing, not modifying anything. Where is the error? Is there a better way to handle the exceptions list?

ArrayList noProcess = new ArrayList();
Iterator itr00 = noProcess.iterator();
Iterator itr01 = noProcess.iterator();
noProcess.add(new Integer("5"));
noProcess.add(new Integer("18"));
....
 boolean include=true;
  for(int i=0;i<archive.length;i++){
    for (int j = 0; j < archive[i].length; j++) {
      while (itr00.hasNext()) {
        if (j == ( (Integer) itr00.next()).intValue())
          include = false;
      }
      if (include) {...

解决方案

You can't alter the contents of an Iterable once you create an iterator on it (other than via the iterator), otherwise you'll get a ConcurrentModificationException as soon as you move the iterator - you create an iterator, then do noProcess.add(new Integer("5"));, then later advance the iterator.

Also, you create two iterators - you shouldn't do that either - it's crazy.

这篇关于concurrentModificationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆