如何在迭代时修改arraylist?我希望只有在我迭代的情况下处理它的答案,请不要回答我可以保存它然后修改它。
for (ListIterator<CardGroup> ShortSeqGroupListIterator = ShortSeqGroupList
.listIterator(); ShortSeqGroupListIterator.hasNext();) {
CardGroup ShortSeqGroup = ShortSeqGroupListIterator.next();
System.out.println("Iteration --- "+ShortSeqGroup.getCardList());
for (ListIterator<CardGroup> cardGroupListIterator = this.cardGroupList
.listIterator(); cardGroupListIterator.hasNext();) {
CardGroup cardGroup = cardGroupListIterator.next();
if (cardGroup.getCardGroupType() == CardGroupType.PURESEQUENCE
|| cardGroup.getCardGroupType() == CardGroupType.SHORTSEQUENCE) {
continue;
}
Listindex = cardGroupListIterator.nextIndex() - 1;
listOfIndex.add(Listindex);
cardGroup.setCardGroupType(CardGroupType.NONE);
this.mergeExtraGroups();
}
ShortSeqGroup.setCardGroupType(CardGroupType.NONE);
this.mergeExtraGroups();
this.markSets();
this.markSequences(false);
int PenaltyPointsShSeq = totalPenaltyOfUser(this.cardGroupList);
PenaltyMapShortSeq.put(PenaltyPointsShSeq, this.cardGroupList);
this.cardGroupList = clonedCardGroupList;
System.out.println("&************************&");
this.print();
}
NavigableMap<Integer, List<CardGroup>> descendedPenaltyMapShortSeq=PenaltyMapShortSeq.descendingMap();
System.out.println(descendedPenaltyMapShortSeq.firstKey());
我想在列表上操作,然后在保存操作后,我需要返回列表的先前状态.. 问题当然是并发修改异常。
this.cardList
是我正在操作的那个,克隆的cardgrouplist
是它的副本......
cardGrouplist
第一个元素再次包含:
一个arraylist
答案 0 :(得分:2)
请勿使用Iterator
,将其切换为正常的for(int i = 0; i < list.length; i++)
循环。
答案 1 :(得分:0)
在迭代时修改集合看起来很麻烦。我建议你迭代收集要添加/删除的项目列表。然后,相应地执行添加/删除收集的项目。