【发布时间】:2019-03-15 03:34:14
【问题描述】:
intLinkList = 2, 4, 6, 8, 10
Iterator itr = intLinkList.iterator()
假设迭代器正在迭代并且当前指向整数 6。
itr current item = 6
itr previous item = 4
itr next item = 8
当 itr 当前指向 Integer 6 时,我使用 Linklists 的 add(Object obj, int index) 方法在 Integer 6 和 Integer 8 之间添加/插入 Integer 7。
我了解此 itr 实例在此修改后无效,因为基础列表已被修改,因此 modCount != expectedModCount。
我的问题是: 使用 LinkedList 的 add 方法进行修改是否会更改 itr.next 指向的项目? 我做了一些阅读,我知道这会引发 ConcurrentModificationException。 但这并不能回答我的问题,如果在迭代器迭代时修改了基础列表,则修改了 itr.next 项。
【问题讨论】:
-
需要注意的是,您可以使用
ListIterator、see here添加元素。
标签: java linked-list iterator listiterator