执行此代码时出现ConcurrentModificationException异常。我无法弄清楚它为什么会发生?Java中的并发修改异常
private void verifyBookingIfAvailable(ArrayList<Integer> list, int id) {
Iterator<Integer> iterator = list.iterator();
while (iterator.hasNext()) {
int value = iterator.next();
if (value == id) {
int index = list.indexOf(id);
if (index != -1) {
list.remove(index);
}
}
}
}
在此先感谢。
你的代码似乎完全等同于'(list.remove(Integer.valueOf(ID))),同时,除非我失去了'东西... – assylias
@assylias。那么,你不会错过任何东西。你绝对正确。根本不需要重复。很好的抓住:) –
@RohitJain只需确保调用'remove(Object)'而不是'remove(int)'。 – assylias