从ListIterator.add API
The element is inserted immediately before the element that would be returned by next(), if any, and after the element that would be returned by previous(), if any. (If the list contains no elements, the new element becomes the sole element on the list.) The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected, and a subsequent call to previous would return the new element.
这里是它如何工作的实践为例
List<String> l = new ArrayList<String>();
l.add("1");
ListIterator<String> i = l.listIterator();
i.add("2");
while (i.hasPrevious()) {
i.previous();
}
i.add("3");
System.out.println(l);
输出
[3, 2, 1]
,我们可以用的ListIterator
做更多的花样
@ jahroy“如何插入使用add()方法在列表的开头?”不,这正是他想要的。如果他尝试在旧的迭代器上调用next或previous,则使用新的迭代器将抛出异常('ConcurrentModificationException')。他将不得不修改他的'for'循环或他用来容纳新调用的任何内容,以便在他调用'add'时未调用'next'。 – Brian
@jahroy你走了。这个答案是正确的。也许Evgeniy应该在调用add之前显示更多的导航,但完全获得另一个列表工具将无法工作 – Bohemian
@Bohemian已经完成 –