<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] ### list集合介紹 list集合怎么定義呢?從概念角度去理解,list集合也叫list列表,存儲的元素有序可重復。有序,就是存儲的位置是固定的,誰先存儲順序就越靠前;可重復,可以存儲重復數據;從代碼角度去理解, list由List接口和List接口的實現類組成。 List接口常用的實現類有哪些? ArrayList、LinkedList和MutableList。kotlin中只有ArrayList和MutableList list集合可以存儲什么類型數據呢? list集合可以存儲各種類型數據。可以存儲諸如Int、Double、String等基本數據類型,也可以存儲其他自定義對象類型,比如自定義的學生Student。 ### list集合4種創建方式 在Kotlin中創建list集合有4種方式,4種方式可以分為兩類,一類是通過構造方法方法創建list集合。另外一種,是通過Kotlin給我們封裝好的方法創建list集合。如下表格,我們將這四種方式列舉了出來。 | **創建list集合方法** | **返回值類型** | **是否可寫** | | ----------------------------- | ------------- | ------------ | | **通過List構造方法創建** | List | 否 | | **listOf()方法** | List | 否 | | **arrayListOf()方法** | ArrayList | 是 | | **mutableListOf()方法** | MutableList | 是 | 我們可以直接看4種方式的對應方法的方法簽名,重點看返回值,方法體的部分我直接去掉了,對應的源碼如下: ``` public inline fun<T> List(size:Int,init:(index:Int)->T):List<T>{} public fun<T> listOf(vararg element:T):List<T>{} public fun arrayListOf(vararg element:T):ArrayList<T>{} public fun <T> mutableListOf(vararg elements: T):MutableList<T>{} ``` 可以通過翻看源碼得知4種創建集合方式上的細微差別,有的是通過Java里面的ArrayList創建,有的是通過asList方法轉換為的集合。這個意義不是很大。 更重要的需要知道,通過List接口構造方法和listOf方法創建的集合是不可寫的,通過arrayListOf方法和mutableListOf方法創建的集合是可寫的。具體使用的時候,忘記是否可寫怎么辦?只需要點進去看看方法的返回值即可,返回List不可寫,返回ArrayList、MutableList則可寫。或者,看每個方法的注釋,返回只讀的list集合,會出現“**read-only** **list**”字樣。比如看List接口構造方法創建集合的注釋: ![](http://upload-images.jianshu.io/upload_images/7368752-d4bbeba8645af389.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 還有就是,如何集合元素很有規律的時候,用List的構造方法創建,比如元素是[1,2,3、4]、[1,2,4,8] 、[1,3,6,9]。listOf方法、arrayListOf方法、mutableListOf方法,創建集合的時候只需要往里面扔具體的元素就可以了。 我們通過代碼演示集合的四種創建方式: ~~~ fun main(args: Array<String>) { val list = List(3, { it * 2 }) println(list) val list1 = listOf("a", "b", "c") println(list1) val list2 = arrayListOf(1, 2, 3) println(list2) val list3 = mutableListOf("a", "b", 'c', 5) println(list3) } ~~~ 運行結果 ``` [0, 2, 4] [a, b, c] [1, 2, 3] [a, b, c, 5] Process finished with exit code 0 ``` 針對以上代碼難理解一點的可能就是第2行,通過List接口構造方法創建list集合,這里涉及到了Lambda表達式。這個可以稍微放一放,學習完Lambda表達式之后在回過頭來看。并且,確實,這種創建方式用的相對較少。 ### List集合的可寫性驗證以及轉換 我們通過代碼驗證集合是否可寫,先驗證集合可寫,參考代碼: ~~~ fun main(args: Array<String>) { val list2 = arrayListOf(1, 2, 3) //可寫性 list2.add(4) println(list2) val list3 = mutableListOf("a", "b", 'c', 5) //可寫性 list3.add(18.4) println(list3) } ~~~ 運行結果 ``` [1, 2, 3, 4] [a, b, c, 5, 18.4] Process finished with exit code 0 ``` 我們再驗證集合不可寫,參考代碼: ![](http://upload-images.jianshu.io/upload_images/7368752-23ae5d170975c101.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) 但是,不可寫集合可以通過totoMutableList轉換為可寫集合,然后在進行寫操作,參考代碼: ~~~ fun main(args: Array<String>) { val list: List<Int> = List(3, { it * 2 }) println("轉換前的list:$list") //轉換添加 list.toMutableList().add(4)//轉換為可寫的list println("轉換后的新的list:$list") println("---------注意下面的添加方式與上面的不同之處-----------") val newList = list.toMutableList() println("已轉換的未進行可寫操作的newList:$newList") newList.add(5) println("已轉換的進行可寫操作后的newList:$newList") println("--------------------------------------------------------") val list1 = listOf("a", "b", "c") println("轉換前的list1:$list1") list1.toMutableList().add("d") println("轉換后的新的list1:$list1") println("---------注意下面的添加方式與上面的不同之處-----------") val newList1 = list1.toMutableList() println("已轉換的未進行可寫操作的newList1:$newList1") newList1.add("d") println("已轉換的進行可寫操作后的newList1:$newList1") } ~~~ 運行結果 ``` 轉換前的list:[0, 2, 4] 轉換后的新的list:[0, 2, 4] ---------注意下面的添加方式與上面的不同之處----------- 已轉換的未進行可寫操作的newList:[0, 2, 4] 已轉換的進行可寫操作后的newList:[0, 2, 4, 5] -------------------------------------------------------- 轉換前的list1:[a, b, c] 轉換后的新的list1:[a, b, c] ---------注意下面的添加方式與上面的不同之處----------- 已轉換的未進行可寫操作的newList1:[a, b, c] 已轉換的進行可寫操作后的newList1:[a, b, c, d] Process finished with exit code 0 ``` * [ ] **疑問**:注意以下代碼的不同之處 ~~~ val list: List<Int> = List(3, { it * 2 }) //轉換添加 list.toMutableList().add(4)//轉換為可寫的list println(list)//[0, 2, 4] val newList = list.toMutableList() newList.add(5) println(list)//[0, 2, 4, 5] ~~~ 為什么會有截然不同的兩種結果? >[success]**注意**:之所以出現兩種結果,是因為執行完`list.toMutableList()`后,生成的新的List,原List沒有變化!而且add方法的返回值是一個Boolean類型,所以`println(list)//[0, 2, 4]`,結果還是原來的list,而經過下面的代碼`val newList = list.toMutableList()`已經生成一個新的list,執行`newList.add(5)`,就是一個正常的寫入的過程,自然就得到`println(list)//[0, 2, 4, 5]` ### list集合數據可重復 list集合中的元素可重復,是list相對于set的一個重要特點。同時,**list集合中可以存儲null元素**,我們通過一個案例驗證下list集合可以包含重復代碼,參考代碼: ![](http://upload-images.jianshu.io/upload_images/7368752-47372c2091f70b58.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ### list集合遍歷 前面我們學習了區間的遍歷,數組的遍歷。那如何遍歷list集合呢?list集合的遍歷和數組的遍歷一樣。 也就是list集合在遍歷的時候,可以普通的for循環,還可以for循環的時候調用withIndex方法,參考代碼: ~~~ fun main(args: Array<String>) { val list = mutableListOf(1, 2, 3, 4, 5) println("---------普通for循環遍歷-----------") for (c in list) { println(c) } println("---------for循環 withIndex遍歷-----------") for (withIndex in list.withIndex()) { println("${withIndex.index} -> ${withIndex.value}") } println("---------for循環 解析析構遍歷-----------") for ((index, value) in list.withIndex()) { println("${index} -> ${value}") } } ~~~ 運行結果 ``` ---------普通for循環遍歷----------- 1 2 3 4 5 ---------for循環 withIndex遍歷----------- 0 -> 1 1 -> 2 2 -> 3 3 -> 4 4 -> 5 ---------for循環 解析析構遍歷----------- 0 -> 1 1 -> 2 2 -> 3 3 -> 4 4 -> 5 Process finished with exit code 0 ``` 當然,還可以通過迭代器以及高階函數進行遍歷操作, 比如 #### 集合list迭代器遍歷 ``` fun main(args: Array<String>) { val list: List<Int> = listOf(1, 2, 3, 4) val iterator1 = list.listIterator() //獲取一個集合的迭代器 val iterator2 = list.listIterator(1) //獲取從索引位置1開始的集合的迭代器 println("遍歷集合中的元素:") while (iterator1.hasNext()) { print(iterator1.next().toString() + "\t") } println("\n" + "從索引1開始遍歷集合中的元素:") while (iterator2.hasNext()) { print(iterator2.next().toString() + "\t") } } ``` 運算結果 ``` 遍歷集合中的元素: 1 2 3 4 從索引1開始遍歷集合中的元素: 2 3 4 ``` #### 集合MutableList迭代器遍歷 ``` fun main(args: Array<String>) { val muList: MutableList<String> = mutableListOf("春天", "夏天", "秋天", "冬天") val iterator1 = muList.listIterator() //獲取集合的迭代器 val iterator2 = muList.listIterator(1) //獲取從位置1開始的集合的迭代器 println("遍歷集合中的元素:") while (iterator1.hasNext()) { print(iterator1.next() + "\t") } println("\n" + "從索引為1處開始遍歷集合中的元素:") while (iterator2.hasNext()) { print(iterator2.next() + "\t") } } ``` 運行結果 ``` 遍歷集合中的元素: 春天 夏天 秋天 冬天 從索引為1處開始遍歷集合中的元素: 夏天 秋天 冬天 ```
                  <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>

                              哎呀哎呀视频在线观看