ArrayList 可以理解为是一个长度可变的集合,在日常开发中使用也比较频繁,这里不写与java中的ArrayList的区别,只单纯的介绍Kotlin中的ArrayList。这里的方法不包括从接口继承来的方法,
AbstractMutableList<E>和RandomAccess的方法会单独介绍。其中比较好玩的方法,一般都来自AbstractMutableList接口
open class ArrayList<E> : AbstractMutableList<E>,RandomAccess可以看出ArrayList实现了两个接口,其中:
AbstractMutableList 使得它的集合长度可变,并且包含了作为一个List应该具备的基本方法RandomAccess使得它支持快速索引,它里面有三个方法equals、hashCode、和toString ArrayList ArrayList(capacity: Int = 0) ArrayList ArrayList(elements: Collection<E>)size 该集合的大小open val size: IntmodCount 继承来的参数,js中表示该集合的结构变化次数var modCount: Int//将元素添加到指定位置
abstract fun add(index: Int, element: E)
//将指定元素添加到集合中
open fun add(element: E): Boolean//将指定集合中的所有元素全部添加到该集合的指定位置
open fun addAll(index: Int, elements: Collection<E>): Boolean//清除该集合里的所有元素
open fun clear()//返回指定位置的元素
open fun get(index: Int): E//返回该元素在集合中第一次出现的位置,如果不存在则返回-1
open fun indexOf(element: E): Int//与`indexOf`相似,返回该元素在集合中最后一次出现的位置,如果不存在则返回-1
open fun lastIndexOf(element: E): Int//去掉该集合中的指定元素
open fun remove(element: E): Boolean//去掉该集合指定位置的元素
abstract fun removeAt(index: Int): E//去掉该集合指定位置区间的所有元素,也就是fromIndex~toIndex之间
open fun removeRange(fromIndex: Int, toIndex: Int)//用指定元素替换掉该集合中指定位置的元素
abstract fun set(index: Int, element: E): E//将集合转换成Array
open fun toArray(): Array<Any?>//返回一个表达该集合的字符串
open fun toString(): String//和其它集合比较是否相等,其中元素必须相等,顺序也必须一致,而且如果两个对象都不为空的话等同于`==`
open fun equals(other: Any?): Boolean//返回该list的哈希值
open fun hashCode(): Int//返回该list的iterator(迭代器),其中包含所有元素
open fun iterator(): MutableIterator<E>//返回该list的listIterator(迭代器),其中包含所有元素,并且是正确顺序
open fun listIterator(): MutableListIterator<E>
//返回该list的listIterator(迭代器),其中包含所有元素,并且是正确顺序,但直接从指定的位置开始遍历
open fun listIterator(index: Int): MutableListIterator<E>//返回一个集合视图,即从fromIndex~toIndex区间的元素视图,视图由该集合支持,所有对视图的操作会直接反映到该集合上,反之亦然
open fun subList(
fromIndex: Int,
toIndex: Int
): MutableList<E>如果哪里写错了,请各位看官及时指出,欢迎留言