Java 迭代器中的ConcurrentModificationException帮助
我在下面的代码段中得到ConcurrentModificationException 当我运行代码时,它运行得很好,但突然它抛出了一个异常,我猜这是由于修改了列表,但我不确定如何修复它Java 迭代器中的ConcurrentModificationException帮助,java,exception,collections,exception-handling,concurrentmodification,Java,Exception,Collections,Exception Handling,Concurrentmodification,我在下面的代码段中得到ConcurrentModificationException 当我运行代码时,它运行得很好,但突然它抛出了一个异常,我猜这是由于修改了列表,但我不确定如何修复它 if (myRulesIncr!=null) { Iterator itReceivedRules = myRulesIncr.iterator(); while (itReceivedRules.hasNext()) { RuleModel currentReceived =
if (myRulesIncr!=null)
{
Iterator itReceivedRules = myRulesIncr.iterator();
while (itReceivedRules.hasNext())
{
RuleModel currentReceived = (RuleModel) itReceivedRules.next();
if (receivedRulesExisting!=null)
{
Iterator itReceivedRulesExisting = receivedRulesExisting.iterator();
while (itReceivedRulesExisting.hasNext())
{
RuleModel currentExisting = (RuleModel) itReceivedRulesExisting.next();
if(currentExisting.getRuleId().equals(currentReceived.getRuleId()))
{
//TODO:replace the rule else add it.
if(currentReceived.getStatus()!="D")
{
//replace the existing rule with the new one
receivedRulesExisting.remove(currentExisting);
receivedRulesExisting.add(currentReceived);
}
else
{
receivedRulesExisting.remove(currentExisting);
}
}
else
{
//Add the new rule to the existing rules
receivedRulesExisting.add(currentReceived);
}
}
}
}
}
请帮我解决这个问题。
ConcurrentModificationException
在外部修改要迭代的集合时抛出,即不是通过迭代器。因此,您需要使用以避免此异常。此外,与其在遍历集合时直接添加到集合中,不如将要添加的项存储在单独的集合中,然后再添加:
List<RuleModel> toBeAdded = new ArrayList<RuleModel>();
if(currentReceived.getStatus()!="D")
{
//replace the existing rule with the new one
itReceivedRulesExisting.remove();
toBeAdded.add(currentReceived);
}
else
{
itReceivedRulesExisting.remove();
}
...
// after the loop terminated:
receivedRulesExisting.addAll(toBeAdded);
List tobeaded=new ArrayList();
如果(currentReceived.getStatus()!=“D”)
{
//用新规则替换现有规则
itReceivedRulesExisting.remove();
tobedded.add(当前已收到);
}
其他的
{
itReceivedRulesExisting.remove();
}
...
//循环终止后:
receivedrules existing.addAll(tobedded);
还请注意,我使用了一个泛型集合-建议这样做,以确保类型安全并避免类似的降级:
Collection<RuleModel> myRulesIncr = ...
...
Iterator<RuleModel> itReceivedRules = myRulesIncr.iterator();
...
RuleModel currentReceived = itReceivedRules.next();
集合myRulesIncr=。。。
...
迭代器itReceivedRules=myRulesIncr.Iterator();
...
RuleModel currentReceived=itReceivedRules.next();
ConcurrentModificationException
在外部(即不是通过迭代器)修改要迭代的集合时抛出。因此,您需要使用以避免此异常。此外,与其在遍历集合时直接添加到集合中,不如将要添加的项存储在单独的集合中,然后再添加:
List<RuleModel> toBeAdded = new ArrayList<RuleModel>();
if(currentReceived.getStatus()!="D")
{
//replace the existing rule with the new one
itReceivedRulesExisting.remove();
toBeAdded.add(currentReceived);
}
else
{
itReceivedRulesExisting.remove();
}
...
// after the loop terminated:
receivedRulesExisting.addAll(toBeAdded);
List tobeaded=new ArrayList();
如果(currentReceived.getStatus()!=“D”)
{
//用新规则替换现有规则
itReceivedRulesExisting.remove();
tobedded.add(当前已收到);
}
其他的
{
itReceivedRulesExisting.remove();
}
...
//循环终止后:
receivedrules existing.addAll(tobedded);
还请注意,我使用了一个泛型集合-建议这样做,以确保类型安全并避免类似的降级:
Collection<RuleModel> myRulesIncr = ...
...
Iterator<RuleModel> itReceivedRules = myRulesIncr.iterator();
...
RuleModel currentReceived = itReceivedRules.next();
集合myRulesIncr=。。。
...
迭代器itReceivedRules=myRulesIncr.Iterator();
...
RuleModel currentReceived=itReceivedRules.next();
这是在多线程环境中吗?如果是,则使用线程安全集合CopyOnWriteArrayList
或集合。synchronizedList
这是在多线程环境中吗?如果是,则使用线程安全集合CopyOnWriteArrayList
或集合。synchronizedList
您使用的集合类型是什么?请先将代码中的制表符转换为空格,然后再将其粘贴到此处的编辑器中。如果不这样做,代码将很难阅读,从而减少您获得有用帮助的机会。您使用的集合类型是什么?请将代码中的制表符转换为空格,然后将其粘贴到此处的编辑器中。如果不这样做,您的代码将很难阅读,从而减少您获得有用帮助的机会。