【发布时间】:2018-06-26 14:24:47
【问题描述】:
我必须从存储中迭代的列表中删除元素。即使使用迭代器,它也会抛出 ConcurrentModificationException。此代码将在活动结束时执行。参考:iter1.remove()。请建议是否有任何解决方法。
List<OfflineCommand> l_loc = (List<OfflineCommand>) Storage.getInstance().readObject("LocationTest");
if (l_loc != null) {
boolean flgSuccess = true;
ListIterator<OfflineCommand> iter1 = l_loc.listIterator();
while (iter1.hasNext()) {
OfflineCommand oc = iter1.next();
flgSuccess = executeOfflineCommand(oc);
if (!flgSuccess) {
break;
} else {
iter1.remove();
}
}
}
在另一个地方,我使用以下代码每分钟将条目添加到同一个列表中。
List<OfflineCommand> l_noAppt = Storage.getInstance().readObject("LocationTest");
if (l_noAppt == null) {
l_noAppt = new ArrayList<>();
}
l_noAppt.add(new OfflineCommand(name, args));
Storage.getInstance().writeObject("LocationTest", l_noAppt);
【问题讨论】:
-
首先你说你的代码将抛出异常。然后你说你不确定它是否会抛出异常。你自相矛盾。请解释一下。
-
我无法预测导致问题的原因。我在项目中使用列表的位置添加了代码。谢谢
标签: codenameone