📜  Kotlin MutableList:mutableListOf()

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

Kotlin MutableList(mutableListOf())

Kotlin MutableList是一个接口和元素的通用集合。 MutableList接口本质上是可变的。它继承了Collection 类的形式。 MutableList接口的方法支持读取和写入功能。一旦声明了MutableList中的元素,就可以在其中添加或删除更多元素,因此它没有固定的大小长度。

要使用MutableList接口,我们使用其称为mutableListOf()mutableListOf ()的函数。

MutableList的元素遵循插入顺序,并包含与数组相同的索引号。

MutableList接口声明

interface MutableList : List, MutableCollection (source)

Kotlin MutableList的功能

MutableList接口中提供了几种方法。下面提到MutableList接口的一些方法。

Function Descriptions
abstract fun add(element: E): Boolean It adds the given element to the collection.
abstract fun add(index: Int, element: E) It adds the element at specified index.
abstract fun addAll(elements: Collection): Boolean It adds all the elements of given collection to current collection.
abstract fun clear() It removes all the elements from this collection.
abstract fun listIterator(): MutableListIterator It returns a list iterator over the elements in proper sequence in current list.
abstract fun listIterator(index: Int): MutableListIterator It returns a list iterator starting from specified index over the elements in list in proper sequence.
abstract fun remove(element: E): Boolean It removes the specified element if it present in current collection.
abstract fun removeAll(elements: Collection): Boolean It removes all the elements from the current list which are also present in the specified collection.
abstract fun removeAt(index: Int): E It removes element at given index from the list.
abstract fun retainAll(elements: Collection): Boolean It retains all the elements within the current collection which are present in given collection.
abstract operator fun set(index: Int, element: E): E It replaces the element and add new at given index with specified element.
abstract fun subList(
fromIndex: Int,
toIndex: Int
): MutableList
It returns part of list from specified fromIndex (inclusive) to toIndex (exclusive) from current list. The returned list is backed by current list, so non-structural changes in the returned list are reflected in current list, and vice-versa.

Kotlin MutableList示例1

我们来看一个使用mutableListOf()函数并遍历其元素的MutableList示例。

fun main(args: Array){
    var mutableList = mutableListOf("Ajay","Vijay","Prakash","Vijay")

    for(element in mutableList){
        println(element)
    }
    println()
    for(index in 0..mutableList.size-1){
        println(mutableList[index])
    }
}

输出:

Ajay
Vijay
Prakash
Vijay

Ajay
Vijay
Prakash
Vijay

Kotlin MutableList示例2

MutableList接口的mutableListOf()函数提供了在声明后添加元素的功能。 MutableList也可以在以后声明为空并添加元素,但是在这种情况下,我们需要定义其通用类型。例如:

fun main(args: Array){
    var mutableList1 = mutableListOf("Ajay","Vijay")
    mutableList1.add("Prakash")
    mutableList1.add("Vijay")

    var mutableList2 = mutableListOf()
    mutableList2.add("Ajeet")
    mutableList2.add("Amit")
    mutableList2.add("Akash")

    for(element in mutableList1){
        println(element)
    }
    println()
    for(element in mutableList2){
        println(element)
    }
}

输出:

Ajay
Vijay
Prakash
Vijay

Ajeet
Amit
Akash

Kotlin MutableList示例3

更具体地说,我们可以提供MutableList接口的通用类型,例如mutableListOf (),mutableListOf (),mutableListOf ()mutableListOf ()仅采用整数值, mutableListOf ()仅采用字符串值,而mutableListOf ()同时采用不同的数据类型值。让我们来看一个例子。

fun main(args: Array){
    var mutableListInt: MutableList = mutableListOf()
    var mutableListString: MutableList = mutableListOf()
    var mutableListAny: MutableList = mutableListOf()

    mutableListInt.add(5)
    mutableListInt.add(7)
    mutableListInt.add(10)
    mutableListInt.add(2,15) //add element 15 at index 2

    mutableListString.add("Ajeet")
    mutableListString.add("Ashu")
    mutableListString.add("Mohan")

    mutableListAny.add("Sunil")
    mutableListAny.add(2)
    mutableListAny.add(5)
    mutableListAny.add("Raj")

    println(".....print Int type.....")
    for(element in mutableListInt){
        println(element)
    }
    println()
    println(".....print String type.....")
    for(element in mutableListString){
        println(element)
    }
    println()
    println(".....print Any type.....")
    for(element in mutableListAny){
        println(element)
    }
}

输出:

.....print Int type.....
5
7
15
10

.....print String type.....
Ajeet
Ashu
Mohan

.....print Any type.....
Sunil
2
5
Raj

Kotlin MutableList示例4

让我们看看使用MutableList的不同函数的接口使用mutableListOf ()函数。

fun main(args: Array){
    var mutableList = mutableListOf()

    mutableList.add("Ajay") // index 0
    mutableList.add("Vijay") // index 1
    mutableList.add("Prakash") // index 2

    var mutableList2 = mutableListOf("Rohan","Raj")
    var mutableList3 = mutableListOf("Dharmesh","Umesh")
    var mutableList4 = mutableListOf("Ajay","Dharmesh","Ashu")

    println(".....mutableList.....")
    for(element in mutableList){
        println(element)
    }
    println(".....mutableList[2].....")
    println(mutableList[2])
    mutableList.add(2,"Rohan")
    println("......mutableList.add(2,\"Rohan\")......")
    for(element in mutableList){
        println(element)
    }
    mutableList.add("Ashu")
    println(".....mutableList.add(\"Ashu\")......")
    for(element in mutableList){
        println(element)
    }
    mutableList.addAll(1,mutableList3)
    println("... mutableList.addAll(1,mutableList3)....")
    for(element in mutableList){
        println(element)
    }
    mutableList.addAll(mutableList2)
    println("...mutableList.addAll(mutableList2)....")
    for(element in mutableList){
        println(element)
    }
    mutableList.remove("Vijay")
    println("...mutableList.remove(\"Vijay\")....")
    for(element in mutableList){
        println(element)
    }
    mutableList.removeAt(2)
    println("....mutableList.removeAt(2)....")
    for(element in mutableList){
        println(element)
    }
    mutableList.removeAll(mutableList2)
    println("....  mutableList.removeAll(mutableList2)....")
    for(element in mutableList){
        println(element)
    }

    println("....mutableList.set(2,\"Ashok\")....")
    mutableList.set(2,"Ashok")
    for(element in mutableList){
        println(element)
    }

    println(".... mutableList.retainAll(mutableList4)....")
    mutableList.retainAll(mutableList4)
    for(element in mutableList){
        println(element)
    }
    println(".... mutableList2.clear()....")
    mutableList2.clear()

    for(element in mutableList2){
        println(element)
    }
    println(".... mutableList2 after mutableList2.clear()....")
    println(mutableList2)

    println("....mutableList.subList(1,2)....")
    println(mutableList.subList(1,2))

}

输出:

.....mutableList.....
Ajay
Vijay
Prakash
.....mutableList[2].....
Prakash
......mutableList.add(2,"Rohan")......
Ajay
Vijay
Rohan
Prakash
.....mutableList.add("Ashu")......
Ajay
Vijay
Rohan
Prakash
Ashu
... mutableList.addAll(1,mutableList3)....
Ajay
Dharmesh
Umesh
Vijay
Rohan
Prakash
Ashu
...mutableList.addAll(mutableList2)....
Ajay
Dharmesh
Umesh
Vijay
Rohan
Prakash
Ashu
Rohan
Raj
...mutableList.remove("Vijay")....
Ajay
Dharmesh
Umesh
Rohan
Prakash
Ashu
Rohan
Raj
....mutableList.removeAt(2)....
Ajay
Dharmesh
Rohan
Prakash
Ashu
Rohan
Raj
....  mutableList.removeAll(mutableList2)....
Ajay
Dharmesh
Prakash
Ashu
....mutableList.set(2,"Ashok")....
Ajay
Dharmesh
Ashok
Ashu
.... mutableList.retainAll(mutableList4)....
Ajay
Dharmesh
Ashu
.... mutableList2.clear()....
.... mutableList2 after mutableList2.clear()....
[]
....mutableList.subList(1,2)....
[Dharmesh]