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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # Kotlin 集合概述 [TOC] Kotlin 標準庫提供了一整套用于管理集合的工具,集合是可變數量(可能為零)的一組條目,各種集合對于解決問題都具有重要意義,并且經常用到。 集合是大多數編程語言的常見概念,因此如果熟悉像 Java 或者 Python 語言的集合,那么可以跳過這一介紹轉到詳細部分。 集合通常包含相同類型的一些(數目也可以為零)對象。集合中的對象稱為**元素或條目**。例如,一個系的所有學生組成一個集合,可以用于計算他們的平均年齡。 以下是 Kotlin 相關的集合類型: * _List_ 是一個**有序**集合,可通過索引(反映元素位置的整數)訪問元素。元素可以在 list 中出現多次(**可重復**)。列表的一個示例是一句話:**有一組字(元素)、這些字的順序很重要并且字可以重復**。 * _Set_ 是唯一元素的集合。它反映了集合(set)的數學抽象:**一組無重復的對象**。一般來說 set 中元素的順序并不重要(無序)。例如,字母表是字母的集合(set)。 * _Map_(或者*字典*)是一組鍵值對。**鍵是唯一的**,每個鍵都剛好映射到一個值。**值可以重復**。map 對于存儲對象之間的邏輯連接非常有用,例如,員工的 ID 與員工的位置。 **Kotlin 讓你可以獨立于所存儲對象的確切類型來操作集合**。換句話說,將 `String` 添加到 `String` list 中的方式與添加 `Int` 或者用戶自定義類的到相應 list 中的方式相同。因此,**Kotlin 標準庫為創建、填充、管理任何類型的集合提供了泛型的(通用的,雙關)接口、類與函數**。這些集合接口與相關函數位于 kotlin.collections 包中。我們來大致了解下其內容。 ## 集合類型 Kotlin 標準庫提供了基本集合類型的實現: set、list 以及 map。 一對接口代表每種集合類型: * 一個 _只讀_ 接口,提供訪問集合元素的操作。 * 一個 _可變_ 接口,通過寫操作擴展相應的只讀接口:添加、刪除和更新其元素。 請注意,更改可變集合不需要它是以 [`var`](http://www.kotlincn.net/docs/reference/basic-syntax.html#defining-variables) 定義的變量:寫操作修改同一個可變集合對象,因此引用不會改變。但是,*如果嘗試對 `val` **集合**重新賦值,你將收到編譯錯誤*。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf("one", "two", "three", "four") numbers.add("five") // 這是可以的 //numbers = mutableListOf("six", "seven") // 編譯錯誤 //sampleEnd } ``` 只讀集合,類型是[型變](http://www.kotlincn.net/docs/reference/generics.html#%E5%9E%8B%E5%8F%98)的。這意味著,如果類 `Rectangle` 繼承自 `Shape`,則可以在需要 `List <Shape>` 的任何地方使用 `List <Rectangle>`。換句話說,**集合類型與元素類型具有相同的子類型關系**。 Maps在值類型上是型變的,但在鍵類型上不是。 反之,**可變集合不是型變的;否則將導致運行時故障**。 如果 `MutableList <Rectangle>` 是`MutableList <Shape>` 的子類型,你可以在其中插入其他 `Shape` 的繼承者(例如,`Circle`),從而違反了它的 `Rectangle` 類型參數。 下面是 Kotlin 集合接口的圖表: ![Collection interfaces hierarchy](http://www.kotlincn.net/assets/images/reference/collections-overview/collections-diagram.png) 讓我們來看看接口及其實現。 ### Collection [`Collection<T>`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html) 是集合層次結構的**根**。此接口表示一個只讀集合的共同行為:檢索大小、檢測是否為成員等等。 `Collection` 繼承自 `Iterable <T>` 接口,它定義了迭代元素的操作。可以使用 `Collection` 作為適用于不同集合類型的函數的參數。對于更具體的情況,請使用 `Collection` 的繼承者: [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html) 與 [`Set`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/index.html)。 ```kotlin fun printAll(strings: Collection<String>) { for(s in strings) print("$s ") println() } fun main() { val stringList = listOf("one", "two", "one") printAll(stringList) val stringSet = setOf("one", "two", "three") printAll(stringSet) } ``` 運行結果 ``` one two one one two three ``` [`MutableCollection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-collection/index.html) 是一個具有寫操作的 `Collection` 接口,例如 `add` 以及 `remove`。 ```kotlin fun List<String>.getShortWordsTo(shortWords: MutableList<String>, maxLength: Int) { this.filterTo(shortWords) { it.length <= maxLength } // throwing away the articles val articles = setOf("a", "A", "an", "An", "the", "The") shortWords -= articles } fun main() { val words = "A long time ago in a galaxy far far away".split(" ") val shortWords = mutableListOf<String>() words.getShortWordsTo(shortWords, 3) println(shortWords) } ``` 運行結果 ``` [ago, in, far, far] ``` ### List [`List<T>`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html) 以指定的順序存儲元素,并**提供使用索引訪問元素的方法**。索引從 0 開始 – 第一個元素的索引 – 直到最后一個元素的索引即 `(list.size - 1)`。 ```kotlin fun main() { //sampleStart val numbers = listOf("one", "two", "three", "four") println("Number of elements: ${numbers.size}") println("Third element: ${numbers.get(2)}") println("Fourth element: ${numbers[3]}") println("Index of element \"two\" ${numbers.indexOf("two")}") //sampleEnd } ``` 運行結果 ``` Number of elements: 4 Third element: three Fourth element: four Index of element "two" 1 ``` **List 元素(包括空值)可以重復**:List 可以包含任意數量的相同對象或單個對象的出現。**如果兩個 List 在相同的位置具有相同大小和相同結構的元素,則認為它們是相等的**。 ```kotlin data class Person(var name: String, var age: Int) fun main() { //sampleStart val bob = Person("Bob", 31) val people = listOf<Person>(Person("Adam", 20), bob, bob) val people2 = listOf<Person>(Person("Adam", 20), Person("Bob", 31), bob) println(people == people2) bob.age = 32 println(people == people2) //sampleEnd } ``` 運行結果 ``` true false ``` [`MutableList`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html) 是可以進行寫操作的 `List`,例如用于在特定位置添加或刪除元素。 ```kotlin fun main() { //sampleStart val numbers = mutableListOf(1, 2, 3, 4) numbers.add(5) numbers.removeAt(1) numbers[0] = 0 numbers.shuffle() println(numbers) //sampleEnd } ``` 運行結果 ``` [5, 3, 4, 0] ``` 如你所見,在某些方面,List 與數組(Array)非常相似。但是,有一個重要的區別:**數組的大小是在初始化時定義的,永遠不會改變; 反之,List 沒有預定義的大小;作為寫操作的結果,可以更改 List 的大小:添加,更新或刪除元素。** >[info]在 Kotlin 中,`List` 的默認實現是 [`ArrayList`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html),可以將其視為可調整大小的數組。 ### Set [`Set<T>`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-set/index.html) 存儲**唯一的元素(不可重復)**;它們的**順序通常是未定義的**。`null` 元素也是唯一的:一個 `Set` 只能包含一個 `null`。當兩個 `set` 具有相同的大小并且對于一個 `set` 中的每個元素都能在另一個 `set` 中存在相同元素,則兩個 `set` 相等。 ```kotlin fun main() { //sampleStart val numbers = setOf(1, 2, 3, 4) println("Number of elements: ${numbers.size}") if (numbers.contains(1)) println("1 is in the set") val numbersBackwards = setOf(4, 3, 2, 1) println("The sets are equal: ${numbers == numbersBackwards}") //sampleEnd } ``` 運行結果 ``` Number of elements: 4 1 is in the set The sets are equal: true ``` [`MutableSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-set/index.html) 是一個帶有來自 `MutableCollection` 的寫操作接口的 `Set`。 `Set`的默認實現 - [`LinkedHashSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-set/index.html) – 保留元素插入的順序。因此,依賴于順序的函數,例如 `first()` 或 `last()`,會在這些 `set` 上返回可預測的結果。 ```kotlin fun main() { //sampleStart val numbers = setOf(1, 2, 3, 4) // LinkedHashSet is the default implementation val numbersBackwards = setOf(4, 3, 2, 1) println(numbers.first() == numbersBackwards.first()) println(numbers.first() == numbersBackwards.last()) //sampleEnd } ``` 運行結果 ``` false true ``` 另一種實現方式 – [`HashSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-set/index.html) – 不聲明元素的順序,所以在它上面調用這些函數會返回不可預測的結果。但是,**`HashSet` 只需要較少的內存來存儲相同數量的元素**。 ### Map [`Map<K, V>`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-map/index.html) 不是 `Collection` 接口的繼承者;但是它也是 Kotlin 的一種集合類型。 `Map` 存儲 _鍵-值_ 對(或 _條目_);**鍵是唯一的,但是不同的鍵可以與相同的值配對(即值可以重復)**。`Map` 接口提供特定的函數進行通過鍵訪問值、搜索鍵和值等操作。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1) println("All keys: ${numbersMap.keys}") println("All values: ${numbersMap.values}") if ("key2" in numbersMap) println("Value by key \"key2\": ${numbersMap["key2"]}") if (1 in numbersMap.values) println("The value 1 is in the map") if (numbersMap.containsValue(1)) println("The value 1 is in the map") // 同上 //sampleEnd } ``` 運行結果 ``` All keys: [key1, key2, key3, key4] All values: [1, 2, 3, 1] Value by key "key2": 2 The value 1 is in the map The value 1 is in the map ``` **無論鍵值對的順序如何,包含相同鍵值對的兩個 `Map` 是相等的**。 ```kotlin fun main() { //sampleStart val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1) val anotherMap = mapOf("key2" to 2, "key1" to 1, "key4" to 1, "key3" to 3) println("The maps are equal: ${numbersMap == anotherMap}") //sampleEnd } ``` 運行結果 ``` The maps are equal: true ``` [`MutableMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-map/index.html) 是一個具有寫操作的 `Map` 接口,可以使用該接口添加一個新的鍵值對或更新給定鍵的值。 ```kotlin fun main() { //sampleStart val numbersMap = mutableMapOf("one" to 1, "two" to 2) numbersMap.put("three", 3) numbersMap["one"] = 11 println(numbersMap) //sampleEnd } ``` 運行結果 ``` {one=11, two=2, three=3} ``` `Map` 的默認實現 – [`LinkedHashMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-map/index.html) – 迭代 Map 時保留元素插入的順序。反之,另一種實現 – [`HashMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-hash-map/index.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>

                              哎呀哎呀视频在线观看