ThinkSSL🔒 一键申购 5分钟快速签发 30天无理由退款 购买更放心 广告
# [迭代器](http://www.kotlincn.net/docs/reference/iterators.html) 对于遍历集合元素,kotlin标准库支持迭代器的常用机制——对象提供对元素的顺序访问,而不暴露集合的底层结构。当您需要逐个处理集合的所有元素时,迭代器非常有用,例如,打印值或对它们进行类似的更新。 迭代器可以从[`Iterable<T>`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterable/index.html)接口的继承者获得,包括`Set`和`List`,通过调用[`iterator()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterable/iterator.html)函数。一旦获得迭代器,它就指向集合的第一个元素;调用[`next()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-iterator/next.html)函数将返回这个元素,并将迭代器位置移动到下一个元素(如果存在的话)。一旦迭代器通过最后一个元素,它就不能再用于检索元素;它也不能复位到任何先前的位置。若要再次迭代集合,请创建一个新的迭代器。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val numbersIterator = numbers.iterator() while (numbersIterator.hasNext()) { println(numbersIterator.next()) } //sampleEnd } ``` 运行结果 ``` one two three four ``` 另一种遍历`Iterable`集合的方法是众所周知的`for`循环。当在集合中使用`for`时,可以隐式地获得迭代器。因此,下面的代码相当于上面的例子: ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") for (item in numbers) { println(item) } //sampleEnd } ``` 运行结果 ``` one two three four ``` 最后,还有一个有用的`foreach()`函数,它允许您自动迭代一个集合,并为每个元素执行给定的代码。所以,同样的例子看起来像这样 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") numbers.forEach { println(it) } //sampleEnd } ``` 运行结果 ``` one two three four ``` ## List 迭代器 对于list集合,有一个特殊的迭代器实现:[ListIterator](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list-iterator/index.html)。它支持双向迭代列表:向前和向后。向后迭代由[hasPrevious()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list-iterator/has-previous.html)和[previous()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list-iterator/previous.html)函数实现。此外,`ListIterator`使用函数[nextIndex()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list-iterator/next-index.html)和[previousIndex()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list-iterator/previous-index.html)提供元素索引的信息。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val listIterator = numbers.listIterator() while (listIterator.hasNext()) listIterator.next() println("Iterating backwards:") while (listIterator.hasPrevious()) { print("Index: ${listIterator.previousIndex()}") println(", value: ${listIterator.previous()}") } //sampleEnd } ``` 运行结果 ``` Iterating backwards: Index: 3, value: four Index: 2, value: three Index: 1, value: two Index: 0, value: one ``` 具有双向迭代的能力,意味着`ListIterator`在到达最后一个元素后仍然可以使用。 ## 可变迭代器 对于迭代可变集合,有[MutableIterator](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-iterator/index.html),它继承了`Iterator`的[remove()](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-iterator/remove.html)函数。因此,您可以在迭代集合时从集合中移除元素。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf("one", "two", "three", "four") val mutableIterator = numbers.iterator() mutableIterator.next() mutableIterator.remove() println("After removal: $numbers") //sampleEnd } ``` 运行结果 ``` After removal: [two, three, four] ``` 除了移除元素之外,[`MutableListIterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list-iterator/index.html) 还可以在迭代列表时插入和替换元素 ```kotlin fun main() { //sampleStart val numbers = mutableListOf("one", "four", "four") val mutableListIterator = numbers.listIterator() mutableListIterator.next() mutableListIterator.add("two") mutableListIterator.next() mutableListIterator.set("three") println(numbers) //sampleEnd } ``` 运行结果 ``` [one, two, three, four] ```