问题描述
我在下面的代码片段中获得ConcurrentModificationException
当我运行代码,它很好,但突然它抛出一个异常,我猜它是由于修改列表,但我不知道如何修复它
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:替换规则,否则添加它。
if(currentReceived.getStatus()!=D)
{
//用新的规则替换现有规则
receivedRulesExisting.remove(currentExisting);
receivedRulesExisting.add(currentReceived);
}
else
{
receivedRulesExisting.remove(currentExisting);
}
}
else
{
//将新规则添加到现有规则
receivedRulesExisting.add(currentReceived);
}
}
}
}
}
$ b
List< RuleModel> toBeAdded = new ArrayList< RuleModel>();
if(currentReceived.getStatus()!=D)
{
//用新的规则替换现有规则
itReceivedRulesExisting.remove
toBeAdded.add(currentReceived);
}
else
{
itReceivedRulesExisting.remove();
}
...
//循环结束后:
receivedRulesExisting.addAll(toBeAdded);
请注意,我使用了通用集合 - 建议这样做,如下所示:
Collection< RuleModel> myRulesIncr = ...
...
迭代器< RuleModel> itReceivedRules = myRulesIncr.iterator();
...
RuleModel currentReceived = itReceivedRules.next();
I get ConcurrentModificationException in the following piece of code
when i running the code it went fine but all of a sudden it throws a exception, i guess its due to the modification of the list, but i'm not sure how to fix it
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);
}
}
}
}
}
Please help me out in this .
ConcurrentModificationException
is thrown when the collection being iterated is modified externally, i.e. not via the iterator. So you need to use Iterator.remove()
to avoid this exception. Moreover, instead of adding directly to the collection while iterating through it, store the items to be added in a separate collection, then add them afterwards:
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);
Note also that I used a generic collection - it is advisable to do so, to ensure type safety and get rid of downcasts like so:
Collection<RuleModel> myRulesIncr = ...
...
Iterator<RuleModel> itReceivedRules = myRulesIncr.iterator();
...
RuleModel currentReceived = itReceivedRules.next();
这篇关于迭代器中的ConcurrentModificationException帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!