1

可能重复:
ArrayList 上的 java.util.ConcurrentModificationException

我正在尝试从线程内的列表中删除项目。我得到了 ConcurrentModificationException. 我从这个链接中读到它与从列表中删除项目有关。我将示例代码放在下面。在我的情况下,如果没有这个例外,我怎样才能正确地做到这一点。

try 
{
    for(Game game:appDeleg.getGlobalGames().getGames())
    {
        if(game.getOwner().getId().equals(params[0]))
        {
            synchronized (appDeleg.getGlobalGames().getGames())
            {
                appDeleg.getGlobalGames().getGames().remove(game);
            }
        }

    }
}

catch (Exception e) 
{
    e.printStackTrace();
    return "noconnection";
}
4

5 回答 5

5

使用迭代器:

Iterator<Game> i = appDeleg.getGlobalGames().getGames().iterator();

Game game;
while(i.hasNext()) {
    game = i.next();
    if(game.getOwner().getId().equals(params[0]))
          i.remove();
    }
}
于 2012-11-23T11:44:39.117 回答
2

增强的 for 循环使用迭代器。如果在迭代时修改了集合,迭代器可能会抛出异常。

在您的情况下,您是通过集合而不是通过迭代器删除项目。

尝试使用标准 for 循环而不是使用隐含的迭代器。假设您的集合是一个数组列表。

ArrayList games = appDeleg.getGlobalGames().getGames();
for(int i=0;i<games.size();i++){
      Game game = games.get(i);
      if(game.getOwner().getId().equals(params[0])){
       games.remove(i);

      }
}
于 2012-11-23T11:52:34.943 回答
1

将要删除的项目收集到一个新列表中,并在遍历原始列表后将其删除。

于 2012-11-23T11:40:45.747 回答
1

synchornized 关键字不会在这里保护您。
您可以从单线程应用程序访问此异常事件(是的 - 您可能会声称异常名称具有误导性)。
这应该使用迭代器遍历列表来完成,
(这将允许您在达到要删除的值时调用 remove),
或者通过将要删除的元素收集到单独的列表中,然后运行 ​​removeAll方法就可以了。

于 2012-11-23T11:40:54.060 回答
1

迭代时不能删除列表项,它会抛出ConcurrentModificationException

如果列表在迭代器创建后的任何时候被结构修改,除了通过迭代器自己的 remove 或 add 方法之外,迭代器将抛出 ConcurrentModificationException。因此,面对并发修改,迭代器快速而干净地失败,而不是在未来不确定的时间冒任意的、非确定性的行为。

文档 ->链接

你应该Iterator从列表中删除项目。

于 2012-11-23T11:43:32.310 回答