Kotlin MutableList是一个接口和通用的元素集合。 MutableList接口本质上是可变的。 它继承了Collection 类的形式。 MutableList接口的方法支持读写功能。 当MutableList中声明了元素,就可以添加更多元素或删除它们,因此MutableList没有固定的大小长度。
要使用MutableList接口,需要使用mutableListOf()或mutableListOf ()函数。
MutableList的元素遵循插入顺序来排序,并与数组具有相同的索引号。
MutableList接口声明
interface MutableList : List, MutableCollection (source)
Kotlin MutableList函数
MutableList接口中有几种方法可用,下面提到MutableList接口的一些方法。
方法
描述
abstract fun add(element: E): Boolean
它将给定元素添加到集合中。
abstract fun add(index: Int, element: E)
它在指定的索引处添加元素。
abstract fun addAll(elements: Collection): Boolean
它将给定集合的所有元素添加到当前集合中。
abstract fun clear()
它会删除此集合中的所有元素。
abstract fun listIterator(): MutableListIterator
它在当前列表中以适当的顺序返回元素的列表迭代器。
abstract fun listIterator(index: Int): MutableListIterator
它以适当的顺序从列表中的元素返回从指定索引开始的列表迭代器。
abstract fun remove(element: E): Boolean
如果指定的元素存在于当前集合中,它将删除它。
abstract fun removeAll(elements: Collection): Boolean
它会删除当前列表中也存在于指定集合中的所有元素。
abstract fun removeAt(index: Int): E
它从列表中删除给定索引处的元素。
abstract fun retainAll(elements: Collection): Boolean
它保留当前集合中存在于给定集合中的所有元素(交集)。