📅  最后修改于: 2021-01-05 07:41:49             🧑  作者: Mango
Kotlin List是一个接口和元素的通用集合。 List接口继承表格Collection
要使用List接口,我们需要使用其名为listOf(),listOf
list的元素遵循插入顺序,并包含与数组相同的索引号。
public interface List : Collection (source)
列表界面中有几种功能可用。 List界面的一些功能在下面提到。
Functions | Descriptions |
---|---|
abstract fun contains(element: E): Boolean | It checks specified element is contained in this collection. |
abstract fun containsAll(elements: Collection |
It checks all elements specified are contained in this collection. |
abstract operator fun get(index: Int): E | It returns the element at given index from the list. |
abstract fun indexOf(element: E): Int | Returns the index of first occurrence of specified element in the list, or -1 if specified element is not present in list. |
abstract fun isEmpty(): Boolean | It returns the true if list is empty, otherwise false. |
abstract fun iterator(): Iterator |
It returns an iterator over the elements of this list. |
abstract fun lastIndexOf(element: E): Int | It returns the index of last occurrence of specified element in the list, or return -1 if specified element is not present in list. |
abstract fun listIterator(): ListIterator |
It returns a list iterator over the elements in proper sequence in current list. |
abstract fun listIterator(index: Int): ListIterator |
It returns a list iterator over the elements in proper sequence in current list, starting at specified index. |
abstract fun subList(fromIndex: Int, toIndex: Int): List |
It returns a part of list between fromIndex (inclusive) to toIndex (exclusive). |
我们来看一个使用listOf()函数的列表示例。
fun main(args: Array){
var list = listOf("Ajay","Vijay","Prakash")//read only, fix-size
for(element in list){
println(element)
}
输出:
Ajay
Vijay
Prakash
在listOf()函数,我们可以同时传递不同类型的数据。列表也可以使用索引范围遍历列表。
fun main(args: Array){
var list = listOf(1,2,3,"Ajay","Vijay","Prakash")//read only, fix-size
for(element in list){
println(element)
}
println()
for(index in 0..list.size-1){
println(list[index])
}
}
输出:
1
2
3
Ajay
Vijay
Prakash
1
2
3
Ajay
Vijay
Prakash
更具体地说,我们可以提供列表的通用类型,例如listOf
fun main(args: Array){
var intList: List = listOf(1,2,3)
var stringList: List = listOf("Ajay","Vijay","Prakash")
var anyList: List = listOf(1,2,3,"Ajay","Vijay","Prakash")
println("print int list")
for(element in intList){
println(element)
}
println()
println("print string list")
for(element in stringList){
println(element)
}
println()
println("print any list")
for(element in anyList){
println(element)
}
}
输出:
print int list
1
2
3
print string list
Ajay
Vijay
Prakash
print any list
1
2
3
Ajay
Vijay
Prakash
让我们看看使用使用listOf
fun main(args: Array){
var stringList: List = listOf("Ajay","Vijay","Prakash","Vijay","Rohan")
var list: List = listOf("Ajay","Vijay","Prakash")
for(element in stringList){
print(element+" ")
}
println()
println(stringList.get(0))
println(stringList.indexOf("Vijay"))
println(stringList.lastIndexOf("Vijay"))
println(stringList.size)
println(stringList.contains("Prakash"))
println(stringList.containsAll(list))
println(stringList.subList(2,4))
println(stringList.isEmpty())
println(stringList.drop(1))
println(stringList.dropLast(2))
}
输出:
Ajay Vijay Prakash Vijay Rohan
Ajay
1
3
5
true
true
[Prakash, Vijay]
false
[Vijay, Prakash, Vijay, Rohan]
[Ajay, Vijay, Prakash]
List接口的局限性在于它是不可变的。声明后不能在列表中添加更多元素。为了解决此限制,Collection框架提供了可变列表。