📜  Kotlin ArrayList:arrayListOf()

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

Kotlin ArrayList:arrayListOf()

arrayListOf()ArrayList类的函数。 ArrayList是可变的,这意味着它提供了读写功能。 arrayListOf()函数返回ArrayList类型。

arrayListOf()函数的语法

inline fun  arrayListOf(): ArrayList (source)
fun  arrayListOf(vararg elements: T): ArrayList (source)

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 arrayListOf()示例1

让我们创建一个简单的arrayListOf()函数示例。

fun main(args: Array){
     var arrayList = arrayListOf(4,7,12)
      for(element in arrayList){
        println(element)
    }
}

输出:

4
7
12

Kotlin arrayListOf()示例2

更具体地说,我们可以定义arrayListOf()函数的通用类型,例如arrayListOf (),arrqayListOf (),arrayListOf ()。让我们来看一个例子。

fun main(args: Array){

    var intArrayList: ArrayList = arrayListOf(1,2,3)
    var stringArrayList: ArrayList = arrayListOf("Ajay","Vijay","Prakash")
    var anyArrayList: ArrayList = arrayListOf(1,2,3,"Ajay","Vijay","Prakash")
    println("print int ArrayList")
    for(element in intArrayList){
        println(element)
    }
    println()
    println("print string ArrayList")
    for(element in stringArrayList){
        println(element)
    }
        println()
    println("print any ArrayList")
    for(element in anyArrayList){
        println(element)
    }
}

输出:

print int ArrayList
1
2
3

print string ArrayList
Ajay
Vijay
Prakash

print any ArrayList
1
2
3
Ajay
Vijay
Prakash

Kotlin arrayListOf()示例3- iterator()函数

ArrayList类的元素也可以使用内置的iterator()函数遍历。例如:

fun main(args: Array){
    val list: ArrayList = arrayListOf()

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

    println(".......print ArrayList.......")
    val itr = list.iterator()
    while(itr.hasNext()) {
        println(itr.next())
    }
}

输出:

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

Kotlin arrayListOf()示例4-get()

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

fun main(args: Array){

    val list: ArrayList = arrayListOf()

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

输出:

.......print list.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......list.get(2).......
Prakash

Kotlin arrayListOf()示例5-set()

arrayListOf()的set()函数用于在给定索引处设置给定元素,并替换该索引处已经存在的任何元素。例如:

fun main(args: Array){

    val list: ArrayList = arrayListOf()

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

    println(".......print list.......")
    for (i in list) {
        println(i)
    }
    println(".......arrayList.set(2,\"Rohan\").......")
    list.set(2,"Rohan")
    println(".......print ArrayList.......")
    for (i in list) {
        println(i)
    }
}

输出:

.......print list.......
Ajay
Vijay
Prakash
.......list.set(2,"Rohan").......
.......print list.......
Ajay
Vijay
Rohan

Kotlin arrayListOf()示例-添加并printEmployee数据

让我们创建ArrayList类的arrayListOf()函数的另一个示例。在此示例中,我们将添加并遍历Employee类数据。在这里,Employee类是定义Employee属性的bean类。

class Employee(var id: Int, var name: String, var phone: Int, var city: String)
fun main(args: Array){
    val arrayList: ArrayList = arrayListOf()
    val e1 = Employee(101, "Ajay", 55555, "Delhi")
    val e2 = Employee(102, "Rahul", 44443, "Mumbai")
    val e3 = Employee(103, "Sanjay", 45422, "Noida")
    arrayList.add(e1)
    arrayList.add(e2)
    arrayList.add(e3)

    for (e in arrayList) {
        println("${e.id} ${e.name} ${e.phone} ${e.city}")
    }
}

输出:

101 Ajay 55555 Delhi
102 Rahul 44443 Mumbai
103 Sanjay 45422 Noida