在对List进行迭代时如果对其进行修改,就会抛出ConcurrentModificationException异常。
for (Entity entity : list) { if (entity.getExitNum() == null) { list.remove(entity); continue; } //...}
单线程的解决办法:
Iteratoriterator = list.iterator();while (iterator.hasNext()) { Entity entity = iterator.next(); if (entity.getExitNum() == null) { iterator.remove(); continue; } //...}
参考: