Java ListIterator 稳健性
Java ListIterator Robustness
我在 Java 中有一个简单的 LinkedList
,它有 10 个元素。在那个状态下,我得到一个普通的 ListIterator
,比如 .listIterator(3)。如果我然后 insert/remove 其他元素进入列表,而不触及我拥有 ListIterator
(也不是迭代器)的元素, ListIterator
仍然有效吗?
如果您在 ListIterator
自己的方法之外修改 java.util.LinkedList
,那么迭代器就会被篡改,如果您再次尝试使用它,将会抛出 ConcurrentModificationException
。
The iterators returned by this class's iterator and listIterator
methods are fail-fast: if the list is structurally modified at any
time after the iterator is created, in any way except through the
Iterator's own remove or add methods, the iterator will throw a
ConcurrentModificationException. Thus, in the face of concurrent
modification, the iterator fails quickly and cleanly, rather than
risking arbitrary, non-deterministic behavior at an undetermined time
in the future.
我在 Java 中有一个简单的 LinkedList
,它有 10 个元素。在那个状态下,我得到一个普通的 ListIterator
,比如 .listIterator(3)。如果我然后 insert/remove 其他元素进入列表,而不触及我拥有 ListIterator
(也不是迭代器)的元素, ListIterator
仍然有效吗?
如果您在 ListIterator
自己的方法之外修改 java.util.LinkedList
,那么迭代器就会被篡改,如果您再次尝试使用它,将会抛出 ConcurrentModificationException
。
The iterators returned by this class's iterator and listIterator methods are fail-fast: if the list is structurally modified at any time after the iterator is created, in any way except through the Iterator's own remove or add methods, the iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.