ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
### [ListIterator](https://lingcoder.gitee.io/onjava8/#/book/12-Collections?id=listiterator) **ListIterator**是一个更强大的**Iterator**子类型,它只能由各种**List**类生成。**Iterator**只能向前移动,而**ListIterator**可以双向移动。它可以生成迭代器在列表中指向位置的后一个和前一个元素的索引,并且可以使用`set()`方法替换它访问过的最近一个元素。可以通过调用`listIterator()`方法来生成指向**List**开头处的**ListIterator**,还可以通过调用`listIterator(n)`创建一个一开始就指向列表索引号为**n**的元素处的**ListIterator**。 下面的示例演示了所有这些能力: ~~~ // collections/ListIteration.java import typeinfo.pets.*; import java.util.*; public class ListIteration { public static void main(String[] args) { List<Pet> pets = Pets.list(8); ListIterator<Pet> it = pets.listIterator(); while(it.hasNext()) System.out.print(it.next() + ", " + it.nextIndex() + ", " + it.previousIndex() + "; "); System.out.println(); // Backwards: while(it.hasPrevious()) System.out.print(it.previous().id() + " "); System.out.println(); System.out.println(pets); it = pets.listIterator(3); while(it.hasNext()) { it.next(); it.set(Pets.get()); } System.out.println(pets); } } /* Output: Rat, 1, 0; Manx, 2, 1; Cymric, 3, 2; Mutt, 4, 3; Pug, 5, 4; Cymric, 6, 5; Pug, 7, 6; Manx, 8, 7; 7 6 5 4 3 2 1 0 [Rat, Manx, Cymric, Mutt, Pug, Cymric, Pug, Manx] [Rat, Manx, Cymric, Cymric, Rat, EgyptianMau, Hamster, EgyptianMau] */ ~~~ `Pets.get()`方法用来从位置 3 开始替换**List**中的所有 Pet 对象。