java ConcurrentModificationException尝试移除列表上的所有内容时(非迭代)
这里有一些奇怪的行为,因为我不是在重复列表
public boolean deleteEvents(ArrayList<EventsModel> list) {
boolean success = false;
synchronized (lock) {
ArrayList<EventsModel> listClone = (ArrayList<EventsModel>) list.clone();
success = processDelete(listClone);
}
return success;
}
private boolean processDelete(List<EventsModel> list) {
boolean success = false;
if (list.size() > 999) {
List<EventsModel> subList = list.subList(0, 998);
list.removeAll(subList); // blows up with ConcurrentModificationException
//
} else {
//
}
return success;
}
我没有正确使用removeAll
# 1 楼答案
您将subList()与removeAll结合使用就是导致此异常的原因。您可以阅读javadoc for subList以了解更多信息