<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # [迭代器](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] ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看