【问题标题】:Current Modification Exception Android Iterators当前修改异常 Android 迭代器
【发布时间】:2013-12-20 11:04:16
【问题描述】:

我收到了CurrentModificationException,但我不知道为什么。这是我的代码:

Iterator<Ficha> itFichas = puzzle.iterator();
Iterator<Integer> index = randomNum.iterator();
while(itFichas.hasNext() && index.hasNext()){
    PAleatorio.add(index.next(),itFichas.next());
}

我之前将puzzle 定义为ArrayList&lt;Integer&gt;。和randomNumPAleatorio 作为ArrayList&lt;Ficha&gt;

为什么会出现异常?

编辑:

ArrayList&lt;Ficha&gt; PAleatorio = finalList;

其中 finalList 是另一个包含多个 Ficha 的列表:

ArrayList<Ficha> finalList = new ArrayList<Ficha>();

finalList.add(new Ficha("1"));
finalList.add(new Ficha("2"));
finalList.add(new Ficha("3"));
finalList.add(new Ficha("4"));

【问题讨论】:

  • 你能出示PAleatorio的声明
  • 只是发布更多的上下文和异常的堆栈跟踪。
  • 很可能,puzzlePAleatorio 指的是同一个对象。
  • @GopalRao 我已将其添加到帖子中。
  • 现在请显示拼图的声明。

标签: java android iterator


【解决方案1】:

“我之前将拼图定义为ArrayList&lt;Integer&gt;。而randomNum, PAleatorio 定义为ArrayList&lt;Ficha&gt;。” 你的意思是puzzleArrayList&lt;Ficha&gt;randomNum ArrayList&lt;Integer&gt; 对吗? 我用一些测试对象重新创建了场景。 java.util.ConcurrentModificationException 的唯一原因可能是 PAleatoriopuzzle 存储相同的引用。

【讨论】:

    【解决方案2】:

    这样做可能会更好:

    int counter = 0;
    do{
        PAleatorio.add(index.get(counter),itFichas.get(counter));
       counter++;
    }while(counter <= itFichas.size() && counter <= index.size());
    

    【讨论】:

      猜你喜欢
      • 2011-01-16
      • 1970-01-01
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-08
      • 2015-09-03
      • 2013-05-06
      相关资源
      最近更新 更多