ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## [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种创建方式](https://www.kancloud.cn/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抽象类