增强for循环进行remove操作
LinkedList<String> roomList = new LinkedList<>(Arrays.asList("1","2","3"));
for (String rl : roomList) {
roomList.remove(rl);
}
for循环本质
Integer i;
for(Iterator iterator = list.iterator(); iterator.hasNext(); System.out.println(i)){
i = (Integer)iterator.next();
}
迭代器
private class ListItr implements ListIterator<E> {
private Node<E> lastReturned;
private Node<E> next;
private int nextIndex;
private int expectedModCount = modCount;
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
如果只删除1次,不会报错。多次就会导致modCount != expectedModCount,从而抛异常