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

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## [List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html#list)接口 [TOC] ### List接口簡介 List接口繼承自Collection接口,是單列集合的一個重要分支,習慣性地會將實現List接口的對象稱為List集合。在**List集合中允許出現重復的元素,所有的元素是以一種線性方式存儲的,在程序中可以通過索引來訪問集合中的指定元素**。另外,**List集合還有一個特點就是元素有序,即元素的存入順序和取出順序一致**。 在Kotlin中,List分為可變集合MutableList(Read&Write,Mutable)和不可變集合List(ReadOnly,Immutable)。 * 可變集合MutableList可以對集合中的元素進行增加和刪除的操作, * 不可變集合List對集合中的元素僅提供只讀操作。 * 可參考,下一節的[list集合4種創建方式](http://www.hmoore.net/alex_wsc/android_kotlin/1037817#list4_7) **在開發過程中,盡可能多用不可變集合List,這樣可以在一定程度上提高內存效率**。 #### List接口的繼承者-----([Inheritors](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html#inheritors)) * [AbstractList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-list/index.html#abstractlist) ``` abstract class AbstractList<out E> : AbstractCollection<E>, List<E> ``` * [MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html) ``` interface MutableList<E> : List<E>, MutableCollection<E> ``` ### 不可變[List](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html#list) 在Kotlin中,不可變List是一個只讀操作的集合,只有size屬性和get()函數。與Java類似,它繼承自Collection進而繼承自Iterable。**不可變List是通過listOf()函數創建的,該函數可以創建空的List,也可以創建包含一個或多個元素的List**。示例代碼如下: ``` val mList: List<Int> = listOf() //創建空的List val mList: List<Int> = listOf(0) //創建包含一個元素的List val mList: List<Int> = listOf(1, 2, 3, 4, 5) //創建包含多個元素的List ``` 在上述代碼中,創建了3個Int類型的List集合,該集合可以不存放元素,也可以直接存放相應的元素。List集合還可以進行以下幾種操作。 #### 查詢操作 List集合的查詢操作主要有判斷集合是否為空,獲取集合中元素的個數以及返回集合中的元素的迭代器。為此,Kotlin提供了一系列方法,如表所示 ![](https://img.kancloud.cn/85/f4/85f4bdc970443ca405468b458c3df48d_1372x273.png) #### 批量操作 在List集合中,經常會判斷一個集合中是否包含某個集合,為此,Kotlin提供了一個`contains All(elements:Collection<@UnsafeVariance E>)`方法。 #### 檢索操作 List集合中的檢索操作主要有查詢集合中某個位置的元素、返回集合中指定元素首次出現的索引、返回指定元素最后一次出現的索引以及返回集合中指定的索引之間的集合。Kotlin中檢索操作的方法如表所示。 ![](https://img.kancloud.cn/fc/dc/fcdc01f61f030c331132950745076937_1362x395.png) #### 遍歷操作 List集合中的遍歷操作主要有返回一個集合的迭代器,以及從指定位置開始返回集合的迭代器。獲取迭代器的相關方法如表所示。 ![](https://img.kancloud.cn/c2/fc/c2fcee18c2a92dbd7daffea35ae36295_1367x159.png) ### 可變[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html#mutablelist)接口 ``` interface MutableList<E> : List<E>, MutableCollection<E> ``` MutableList是List集合中的可變集合,MutableList<E>接口繼承于List<E>接口和Mutable Collection<E>接口,增加了對集合中元素的添加及刪除的操作。可變MutableList集合是使用mutableListOf()函數來創建對象的,具體代碼如下: ``` val muList: MutableList<Int> = mutableListOf(1, 2, 3) ``` 上述代碼中創建了一個可變的List集合,這個集合與不可變集合的操作類似,具體如下。 >[info]注意:MutableList接口繼承自List接口,所以上面List接口的方法也適用于MutableList接口,具體可查看官網[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html#mutablelist) #### 查詢操作 MutableList集合的查詢操作與List集合一樣,主要有判斷集合是否為空、獲取集合中元素的數量以及返回集合中的元素的迭代器,相關方法如表所示。 ![](https://img.kancloud.cn/8f/cf/8fcf274bea7456bba0b2d0d5ceac70ed_1375x306.png) #### 修改操作 MutableList集合可以對該集合中的元素進行修改操作,這些修改操作主要有向集合中添加一個元素、在指定位置添加元素、移除一個元素、移除指定位置的元素以及替換指定位置的元素,相關方法如表所示 ![](https://img.kancloud.cn/6c/58/6c587eac1bc0374ebf32937a811d69cb_1364x315.png) #### 批量操作 MutableList集合的批量操作主要有判斷集合中是否包含另一個集合、向集合中添加一個集合、移除集合中的一個集合以及將集合中的所有元素清空,相關方法如表所示。 ![](https://img.kancloud.cn/44/44/4444c39014f520596e210dc5ff64bea4_1367x443.png) #### 遍歷操作 MutableList集合中的遍歷操作主要有返回一個集合的迭代器與返回從指定位置開始的集合的迭代器,如表所示。 ![](https://img.kancloud.cn/24/5f/245f556827060da2d724702086c21af2_1367x152.png) ### MutableList的繼承者-----[(Inheritors)](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html#inheritors) 從源碼或者[MutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-mutable-list/index.html#mutablelist),知道MutableList的繼承者有抽象類[AbstractMutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-mutable-list/index.html)和[ArrayList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html#arraylist) #### [AbstractMutableList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-mutable-list/index.html) ``` abstract class AbstractMutableList<E> : MutableList<E>//kotlin 1.2 ``` 它的繼承者[(Inheritors)](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-mutable-list/index.html#inheritors)是[ArrayList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html#arraylist),不過這是kotlin1.1時(適用于js)的,kotlin1.3時,ArrayList的繼承框架結構就變成下面的結構了。詳情可查看官方網站[ArrayList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html#arraylist) #### [ArrayList](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-list/index.html#arraylist) ``` //kotlin 1.3 class ArrayList<E> : MutableList<E>, RandomAccess, AbstractMutableCollection<E> ``` 可以看出:類ArrayList,實現了MutableList接口和RandomAccess接口,而且還繼承自AbstractMutableCollection抽象類
                  <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>

                              哎呀哎呀视频在线观看