<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之旅 廣告
                # 構造集合 [TOC] ## 由元素構造 創建集合的最常用方法是使用標準庫函數 [`listOf<T>()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/list-of.html)、[`setOf<T>()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/set-of.html)、[`mutableListOf<T>()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-list-of.html)、[`mutableSetOf<T>()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-set-of.html)。 如果以逗號分隔的集合元素列表作為參數,編譯器會自動檢測元素類型。創建空集合時,須明確指定類型。 ```kotlin val numbersSet = setOf("one", "two", "three", "four") val emptySet = mutableSetOf<String>() ``` 同樣的,Map 也有這樣的函數 [`mapOf()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/map-of.html) 與 [`mutableMapOf()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/mutable-map-of.html)。映射的鍵和值作為 `Pair` 對象傳遞(通常使用中綴函數 `to` 創建)。 ```kotlin val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1) ``` >[success]注意,`to` 符號創建了一個**短時存活的 `Pair` 對象**,因此**建議僅在性能不重要時才使用它。為避免過多的內存使用,請使用其他方法**。例如,可以創建可寫 Map 并使用寫入操作填充它。[`apply()`](scope-functions.html#apply) 函數可以幫助保持初始化流暢。 ```kotlin val numbersMap = mutableMapOf<String, String>().apply { this["one"] = "1"; this["two"] = "2" } println(numbersMap) ``` 運行結果 ``` {one=1, two=2} ``` ## 空集合 還有用于**創建沒有任何元素的集合**的函數:[`emptyList()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/empty-list.html)、[`emptySet()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/empty-set.html) 與 [`emptyMap()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/empty-map.html)。創建空集合時,應指定集合將包含的元素類型。 ```kotlin val empty = emptyList<String>() ``` 示例 ``` fun main() { val map = emptyMap<String, Int>() println("map.isEmpty() is ${map.isEmpty()}") // true println("map:${map}") val anotherMap = mapOf<String, Int>() // Empty maps are equal println("map == anotherMap is ${map == anotherMap}") // true println("anotherMap=${anotherMap}") } ``` 運行結果 ``` {one=1, two=2} map.isEmpty() is true map:{} map == anotherMap is true anotherMap={} ``` ## list 的初始化函數 對于 List,有一個接受 List 的大小與初始化函數的構造函數,該初始化函數根據索引定義元素的值。 ```kotlin fun main() { //sampleStart val doubled = List(3, { it * 2 }) // 如果你想操作這個集合,應使用 MutableList println(doubled) //sampleEnd } ``` 運行結果 ``` [0, 2, 4] ``` ## 具體類型構造函數 要創建具體類型的集合,例如 `ArrayList` 或 `LinkedList`,可以使用這些類型的構造函數。類似的構造函數對于 `Set` 與 `Map` 的各實現中均有提供。 ```kotlin val linkedList = LinkedList<String>(listOf("one", "two", "three")) val presizedSet = HashSet<Int>(32) ``` ## 復制 要創建與現有集合具有相同元素的集合,可以使用復制操作。標準庫中的集合復制操作創建了具有相同元素引用的 *shallow*(淺)復制集合。因此,對集合元素所做的更改會反映在其所有副本中。 在特定時刻通過集合復制函數,例如[`toList()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-list.html)、[`toMutableList()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-mutable-list.html)、[`toSet()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/to-set.html) 等等。創建了集合的快照。結果是**創建了一個具有相同元素的新集合。如果在源集合中添加或刪除元素,則不會影響副本。副本也可以獨立于源集合進行更改**。 ```kotlin fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val copyList = sourceList.toMutableList() val readOnlyCopyList = sourceList.toList() sourceList.add(4) println("Copy size: ${copyList.size}") //readOnlyCopyList.add(4) // 編譯異常 println("Read-only copy size: ${readOnlyCopyList.size}") //sampleEnd } ``` 運行結果 ``` Copy size: 3 Read-only copy size: 3 ``` 這些函數還可用于將集合轉換為其他類型,例如根據 List 構建 Set,反之亦然。 ```kotlin fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val copySet = sourceList.toMutableSet() copySet.add(3) copySet.add(4) println(copySet) //sampleEnd } ``` 運行結果 ``` [1, 2, 3, 4] ``` 或者,**可以創建對同一集合實例的新引用。使用現有集合初始化集合變量時,將創建新引用**。因此,**當通過引用更改集合實例時,更改將反映在其所有引用中**。 ```kotlin fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val referenceList = sourceList referenceList.add(4) println("Source size: ${sourceList.size}") //sampleEnd } ``` 運行結果 ``` Source size: 4 ``` 集合的初始化可用于限制其可變性。例如,如果構建了一個 `MutableList` 的 `List` 引用,當你試圖通過此引用修改集合的時候,編譯器會拋出錯誤。 ```kotlin fun main() { //sampleStart val sourceList = mutableListOf(1, 2, 3) val referenceList: List<Int> = sourceList //referenceList.add(4) // 編譯錯誤 sourceList.add(4) println(referenceList) // 顯示 sourceList 當前狀態 //sampleEnd } ``` 運行結果 ``` [1, 2, 3, 4] ``` ## 調用其他集合的函數 可以通過其他集合各種操作的結果來創建集合。例如,[過濾](http://www.kotlincn.net/docs/reference/collection-filtering.html)列表會創建與過濾器匹配的新元素列表: ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") val longerThan3 = numbers.filter { it.length > 3 } println(longerThan3) //sampleEnd } ``` 運行結果 ``` [three, four] ``` [Mapping](http://www.kotlincn.net/docs/reference/collection-transformations.html#%E6%98%A0%E5%B0%84)生成轉換結果列表: ```kotlin fun main() { //sampleStart val numbers = setOf(1, 2, 3) println(numbers.map { it * 3 }) println(numbers.mapIndexed { idx, value -> value * idx }) //sampleEnd } ``` 運行結果 ``` [3, 6, 9] [0, 2, 6] ``` [關聯](http://www.kotlincn.net/docs/reference/collection-transformations.html#%E5%85%B3%E8%81%94)生成 Map: ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") println(numbers.associateWith { it.length }) //sampleEnd } ``` 運行結果 ``` {one=3, two=3, three=5, four=4} ``` 有關 Kotlin 中集合操作的更多信息,參見[集合操作概述](http://www.kotlincn.net/docs/reference/collection-operations.html).
                  <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>

                              哎呀哎呀视频在线观看