<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>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 取單個元素 [TOC] Kotlin 集合提供了一套從集合中檢索單個元素的函數。此頁面描述的函數適用于 list 和 set。 正如 [list 的定義](collections-overview.html)所言,list 是有序集合。因此,list 中的每個元素都有其位置可供你引用。除了此頁面上描述的函數外,list 還提供了更廣泛的一套方法去按索引檢索和搜索元素。有關更多詳細信息,請參見 [List 相關操作](list-operations.html)。 反過來,從[定義](collections-overview.html)來看,set 并不是有序集合。但是,Kotlin 中的 `Set` 按某些順序存儲元素。這些可以是插入順序(在 `LinkedHashSet` 中)、自然排序順序(在 `SortedSet` 中)或者其他順序。一組元素的順序也可以是未知的。在這種情況下,元素仍會以某種順序排序,因此,依賴元素位置的函數仍會返回其結果。但是,除非調用者知道所使用的 `Set` 的具體實現,否則這些結果對于調用者是不可預測的。 ## 按位置取 為了檢索特定位置的元素,有一個函數 [`elementAt()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/element-at.html)。用一個整數作為參數來調用它,你會得到給定位置的集合元素。第一個元素的位置是 `0`,最后一個元素的位置是 `(size - 1)`。 `elementAt()` 對于不提供索引訪問或非靜態已知提供索引訪問的集合很有用。在使用 `List` 的情況下,使用[索引訪問操作符](list-operations.html#按索引取元素) (`get()` 或 `[]`)更為習慣。 ```kotlin fun main() { //sampleStart val numbers = linkedSetOf("one", "two", "three", "four", "five") println(numbers.elementAt(3)) val numbersSortedSet = sortedSetOf("one", "two", "three", "four") println(numbersSortedSet.elementAt(0)) // 元素以升序存儲 //sampleEnd } ``` 還有一些有用的別名來檢索集合的第一個和最后一個元素:[`first()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html) 和 [`last()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last.html)。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five") println(numbers.first()) println(numbers.last()) //sampleEnd } ``` 為了避免在檢索位置不存在的元素時出現異常,請使用 `elementAt()` 的安全變體: * 當指定位置超出集合范圍時,[`elementAtOrNull()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/element-at-or-null.html) 返回 null。 * [`elementAtOrElse()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/element-at-or-else.html) 還接受一個 lambda 表達式,該表達式能將一個 `Int` 參數映射為一個集合元素類型的實例。當使用一個越界位置來調用時,`elementAtOrElse()` 返回對給定值調用該 lambda 表達式的結果。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five") println(numbers.elementAtOrNull(5)) println(numbers.elementAtOrElse(5) { index -> "The value for index $index is undefined"}) //sampleEnd } ``` ## 按條件取 函數 [`first()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first.html) 和 [`last()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last.html) 還可以讓你在集合中搜索與給定謂詞匹配的元素。當你使用測試集合元素的謂詞調用 `first()` 時,你會得到對其調用謂詞產生 `true` 的第一個元素。反過來,帶有一個謂詞的 `last()` 返回與其匹配的最后一個元素。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five", "six") println(numbers.first { it.length > 3 }) println(numbers.last { it.startsWith("f") }) //sampleEnd } ``` 如果沒有元素與謂詞匹配,兩個函數都會拋異常。為了避免它們,請改用 [`firstOrNull()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/first-or-null.html) 和 [`lastOrNull()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/last-or-null.html):如果找不到匹配的元素,它們將返回 `null`。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five", "six") println(numbers.firstOrNull { it.length > 6 }) //sampleEnd } ``` 或者,如果別名更適合你的情況,那么可以使用別名: * 使用 [`find()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/find.html) 代替 `firstOrNull()` * 使用 [`findLast()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/find-last.html) 代替 `lastOrNull()` ```kotlin fun main() { //sampleStart val numbers = listOf(1, 2, 3, 4) println(numbers.find { it % 2 == 0 }) println(numbers.findLast { it % 2 == 0 }) //sampleEnd } ``` ## 隨機取元素 如果需要檢索集合的一個隨機元素,那么請調用 [`random()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/random.html) 函數。你可以不帶參數或者使用一個 [`Random`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.random/-random/index.html) 對象作為隨機源來調用它。 ```kotlin fun main() { //sampleStart val numbers = listOf(1, 2, 3, 4) println(numbers.random()) //sampleEnd } ``` ## 檢測存在與否 如需檢查集合中某個元素的存在,可以使用 [`contains()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains.html) 函數。如果存在一個集合元素等于(`equals()`)函數參數,那么它返回 `true`。你可以使用 `in` 關鍵字以操作符的形式調用 `contains()`。 如需一次檢查多個實例的存在,可以使用這些實例的集合作為參數調用 [`containsAll()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/contains-all.html)。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five", "six") println(numbers.contains("four")) println("zero" in numbers) println(numbers.containsAll(listOf("four", "two"))) println(numbers.containsAll(listOf("one", "zero"))) //sampleEnd } ``` 此外,你可以通過調用 [`isEmpty()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/is-empty.html) 和 [`isNotEmpty()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/is-not-empty.html) 來檢查集合中是否包含任何元素。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four", "five", "six") println(numbers.isEmpty()) println(numbers.isNotEmpty()) val empty = emptyList<String>() println(empty.isEmpty()) println(empty.isNotEmpty()) //sampleEnd } ```
                  <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>

                              哎呀哎呀视频在线观看