来自 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 做更多的技巧