📜  Kotlin ArrayList

📅  最后修改于: 2021-01-05 07:44:03             🧑  作者: Mango

Kotlin ArrayList类

Kotlin ArrayList类用于创建动态数组。这意味着可以根据需要增加或减少ArrayList类的大小。 ArrayList类提供读取和写入功能。

Kotlin ArrayList类遵循插入顺序的顺序。 ArrayList类是不同步的,它可能包含重复的元素。 ArrayList类的元素是按索引工作的,因此可以随机访问。

Kotlin ArrayList的构造方法

Constructor Description
ArrayList() It is used to create an empty ArrayList
ArrayList(capacity: Int) It is used to create an ArrayList of specified capacity.
ArrayList(elements: Collection) It is used to create an ArrayList filled from the elements of collection.

Kotlin ArrayList的功能

Function Description
open fun add(element: E): Boolean It is used to add the specific element into the collection.
open fun add(index: Int, element: E) It is used to insert an element at specific index.
open fun addAll(elements: Collection): Boolean It is used to add all the elements in the specified collection to current collection.
open fun addAll(index: Int, elements: Collection): Boolean It is used to add all the elements of specified collection into the current list at the specified index.
open fun clear() It is used to removes all elements from the collection.
open fun get(index: Int): E It is used to return the element at specified index in the list.
open fun indexOf(element: E): Int It is used to return the index of first occurrence of specified element in the list or return -1 if the specified element in not present in the list.
open fun lastIndexOf(element: E): Int It is used to return the last occurrence of given element from the list or it returns -1 if the given element is not present in the list.
open fun remove(element: E): Boolean It is used to remove a single instance of the specific element from current collection, if it is available.
open fun removeAt(index: Int): E It is used to remove the specific index element from the list.
open fun removeRange(startIndex: Int, endIndex: Int) Its remove the range of elements starting from startIndex to endIndex in which endIndex is not includes.
open fun set(index: Int, element: E): E It is used to replaces the element from the specified position from current list with the specified element.
open fun toArray(): Array It is used to return new array of type Array with the elements of this collection.
open fun toString(): String It is used to returns a string representation of the object.
fun trimToSize() It does nothing in this ArrayList implementation.

Kotlin ArrayList示例1-空ArrayList

让我们创建一个简单的ArrayList类示例示例,该示例使用String的空ArrayList定义并稍后添加元素。

fun main(args: Array){

    val arrayList = ArrayList()//Creating an empty arraylist
    arrayList.add("Ajay")//Adding object in arraylist
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
}

输出:

......print ArrayList......
Ajay
Vijay
Prakash
Rohan
Vijay

Kotlin ArrayList示例2-初始化ArrayList容量

让我们创建一个具有初始容量的ArrayList类。 ArrayList类的容量不是固定的,以后可以根据需要在程序中进行更改。

fun main(args: Array){

    val arrayList1 = ArrayList(5)
    arrayList1.add("Ajay")//Adding object in arraylist
    arrayList1.add("Vijay")
    arrayList1.add("Prakash")
    arrayList1.add("Rohan")
    arrayList1.add("Vijay")
    println(".......print ArrayList1......")
    for (i in arrayList1) {
        println(i)
    }
    println("size of arrayList1 = "+arrayList1.size)
    val arrayList2 = ArrayList(5)
    arrayList2.add(14)
    arrayList2.add(20)
    arrayList2.add(80)
    println("......print ArrayList2......")
    for (i in arrayList2) {
        println(i)
    }
    println("size of arrayList2 = "+arrayList2.size)
}

输出:

.......print ArrayList1......
Ajay
Vijay
Prakash
Rohan
Vijay
size of arrayList1 = 5
......print ArrayList2......
14
20
80
size of arrayList2 = 3

Kotlin ArrayList示例3-使用集合在ArrayList中填充元素

Kotlin ArratList类中的元素也可以使用其他集合添加。对于ArrayList类中的更多特定类,它由其泛型类型声明。 ArrayList类的元素也可以使用iterator()函数遍历。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)
    var list: MutableList = mutableListOf()

    list.add("Ajay")
    list.add("Vijay")
    list.add("Prakash")

    arrayList.addAll(list)
    println("......print ArrayList......")
    val itr = arrayList.iterator()
    while(itr.hasNext()) {
        println(itr.next())
    }
    println("size of arrayList = "+arrayList.size)
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
size of arrayList = 3

Kotlin ArrayList示例4-get()

ArrayList类的get()函数用于检索给定指定索引处存在的元素。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.get(2).......")
    println( arrayList.get(2))
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.get(2).......
Prakash

Kotlin ArrayList示例5-set()

ArrayList类的set()函数用于在给定索引处设置给定元素,如果在给定索引处存在任何元素,则将其替换。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.set(2,\"Ashu\").......")
    arrayList.set(2,"Ashu")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.set(2,"Ashu").......
.......print ArrayList.......
Ajay
Vijay
Ashu
Rohan
Vijay

Kotlin ArrayList示例6-indexOf()

ArrayList类的indexOf()函数用于检索第一次出现的元素的索引值,如果指定的元素不在列表中,则返回-1。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
        println(".......arrayList.indexOf(\"Vijay\").......")
    println(arrayList.indexOf("Vijay"))
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.indexOf("Vijay").......
1

Kotlin ArrayList示例7-lastIndexOf()

ArrayList类的lastindexOf()函数用于检索元素的最后一次出现的索引值,如果指定的元素不在列表中,则返回-1。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.lastIndexOf(\"Vijay\").......")
    println(arrayList.lastIndexOf("Vijay"))
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.lastIndexOf("Vijay").......
4

Kotlin ArrayList示例8-remove()

ArrayList类的remove()函数用于删除列表中首次出现的元素。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.remove(\"Vijay\").......")
    arrayList.remove("Vijay")
    for (i in arrayList) {
        println(i)
    }
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.remove("Vijay").......
Ajay
Prakash
Rohan
Vijay

Kotlin ArrayList示例9-removeAt()

ArrayList类的removeAt()函数用于从列表中删除指定索引的元素。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.remove(3).......")
    arrayList.removeAt(3)
    for (i in arrayList) {
        println(i)
    }
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.remove(3).......
Ajay
Vijay
Prakash
Vijay

Kotlin ArrayList示例10-clear()

ArrayList类的clear()函数用于删除(清除)列表的所有元素。例如:

fun main(args: Array){

    val arrayList: ArrayList = ArrayList(5)

    arrayList.add("Ajay")
    arrayList.add("Vijay")
    arrayList.add("Prakash")
    arrayList.add("Rohan")
    arrayList.add("Vijay")
    println(".......print ArrayList.......")
    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.clear().......")
    arrayList.clear()

    for (i in arrayList) {
        println(i)
    }
    println(".......arrayList.......")
    println(arrayList)
}

输出:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.clear().......
.......arrayList.......
[]