📜  Kotlin 列表:listOf()

📅  最后修改于: 2022-05-13 01:55:25.116000             🧑  作者: Mango

Kotlin 列表:listOf()

列表是元素的通用有序集合。 Kotlin 有两种类型的列表,不可变列表(不能修改)和可变列表(可以修改)。
只读列表是用 listOf() 创建的,它的元素不能被修改,而可变列表是用 mutableListOf() 方法创建的,我们改变或修改列表的元素。
列表的 Kotlin 程序包含整数 -

Java
fun main(args: Array) {
    val a = listOf('1', '2', '3')
    println(a.size)
    println(a.indexOf('2'))
    println(a[2])
}


Java
fun main(args: Array) {
    //creating list of strings
    val a = listOf("Ram", "Shyam", "Raja", "Rani")
    println("The size of the list is: "+a.size)
    println("The index of the element Raja is: "+a.indexOf("Raja"))
    println("The element at index "+a[2])
    for(i in a.indices){
        println(a[i])
    }
}


Java
fun main(args: Array)
{
    val numbers = listOf(1, 5, 7, 32, 0, 21, 1, 6, 10)
 
    val num1 = numbers.get(0)
    println(num1)
 
    val num2 = numbers[7]
    println(num2)
 
    val index1 = numbers.indexOf(1)
    println("The first index of number is $index1")
 
    val index2 = numbers.lastIndexOf(1)
    println("The last index of number is $index2")
 
    val index3 = numbers.lastIndex
    println("The last index of the list is $index3")
}


Java
fun main(args: Array)
{
    val numbers = listOf(1, 5, 7, 32, 0, 21, 1, 6, 10)
    println(numbers.first())
    println(numbers.last())
}


Java
fun main(args: Array)
{
    val names = listOf("Gopal", "Asad", "Shubham", "Aditya",
        "Devarsh", "Nikhil", "Gagan")
 
    // method 1
    for (name in names) {
        print("$name, ")
    }
    println()
     
    // method 2
    for (i in 0 until names.size) {
        print("${names[i]} ")
    }
    println()
    
    // method 3
    names.forEachIndexed({i, j -> println("names[$i] = $j")})
 
    // method 4
    val it: ListIterator = names.listIterator()
    while (it.hasNext()) {
        val i = it.next()
        print("$i ")
    }
    println()
}


Java
fun main(args: Array)
{
    val list = listOf(8, 4, 7, 1, 2, 3, 0, 5, 6 )
 
    val asc = list.sorted()
    println(asc)
 
    val desc = list.sortedDescending()
    println(desc)
}


Java
fun main(args: Array)
{
    val list = listOf(8, 4, 7, 1, 2, 3, 0, 5, 6 )
 
    val res = list.contains(0)
 
    if (res)
        println("The list contains 0")
    else
        println("The list does not contain 0")
 
    val result = list.containsAll(listOf(3, -1))
 
    if (result)
        println("The list contains 3 and -1")
    else
        println("The list does not contain 3 and -1")
}


输出:

3
1
3

列表的 Kotlin 程序包含字符串 -

Java

fun main(args: Array) {
    //creating list of strings
    val a = listOf("Ram", "Shyam", "Raja", "Rani")
    println("The size of the list is: "+a.size)
    println("The index of the element Raja is: "+a.indexOf("Raja"))
    println("The element at index "+a[2])
    for(i in a.indices){
        println(a[i])
    }
}

输出:

The size of the list is: 4
The index of the element Raja is: 2
The element at index Raja
Ram
Shyam
Raja
Rani

Kotlin 中列表元素的索引 –

列表的每个元素都有一个索引。第一个元素的索引为零 (0),最后一个元素的索引为
len – 1,其中 'len' 是列表的长度。

Java

fun main(args: Array)
{
    val numbers = listOf(1, 5, 7, 32, 0, 21, 1, 6, 10)
 
    val num1 = numbers.get(0)
    println(num1)
 
    val num2 = numbers[7]
    println(num2)
 
    val index1 = numbers.indexOf(1)
    println("The first index of number is $index1")
 
    val index2 = numbers.lastIndexOf(1)
    println("The last index of number is $index2")
 
    val index3 = numbers.lastIndex
    println("The last index of the list is $index3")
}

输出:

1
6
The first index of number is 0
The last index of number is 6
The last index of the list is 8

第一个和最后一个元素 -

我们可以在不使用 get() 方法的情况下检索列表的第一个和最后一个元素。
考虑前面的示例,如果我们在第 1 行之后包含以下代码。 17

Java

fun main(args: Array)
{
    val numbers = listOf(1, 5, 7, 32, 0, 21, 1, 6, 10)
    println(numbers.first())
    println(numbers.last())
}

输出:

1
10

列出迭代方法 –

这是一个一个访问列表元素的过程。
在 Kotlin 中有几种方法可以做到这一点。

Java

fun main(args: Array)
{
    val names = listOf("Gopal", "Asad", "Shubham", "Aditya",
        "Devarsh", "Nikhil", "Gagan")
 
    // method 1
    for (name in names) {
        print("$name, ")
    }
    println()
     
    // method 2
    for (i in 0 until names.size) {
        print("${names[i]} ")
    }
    println()
    
    // method 3
    names.forEachIndexed({i, j -> println("names[$i] = $j")})
 
    // method 4
    val it: ListIterator = names.listIterator()
    while (it.hasNext()) {
        val i = it.next()
        print("$i ")
    }
    println()
}

输出:

Gopal, Asad, Shubham, Aditya, Devarsh, Nikhil, Gagan, 
Gopal Asad Shubham Aditya Devarsh Nikhil Gagan 
names[0] = Gopal
names[1] = Asad
names[2] = Shubham
names[3] = Aditya
names[4] = Devarsh
names[5] = Nikhil
names[6] = Gagan
Gopal Asad Shubham Aditya Devarsh Nikhil Gagan 

解释:

for (name in names) {

        print("$name, ")
    }

for 循环遍历列表。在每个循环中,变量“name”指向列表中的下一个元素。

for (i in 0 until names.size) {

        print("${names[i]} ")
    }

此方法使用列表的大小。 until 关键字创建一个列表索引范围。

names.forEachIndexed({i, j -> println("namess[$i] = $j")})

使用 forEachIndexed() 方法,我们循环遍历具有在每次迭代中可用的索引和值的列表。

val it: ListIterator = names.listIterator()

    while (it.hasNext()) {

        val i = it.next()
        print("$i ")
    }

这里我们使用 ListIterator 来遍历列表。

对列表中的元素进行排序 -

以下示例显示如何按升序或降序对列表进行排序。

Java

fun main(args: Array)
{
    val list = listOf(8, 4, 7, 1, 2, 3, 0, 5, 6 )
 
    val asc = list.sorted()
    println(asc)
 
    val desc = list.sortedDescending()
    println(desc)
}

输出:

[0, 1, 2, 3, 4, 5, 6, 7, 8]
[8, 7, 6, 5, 4, 3, 2, 1, 0]

解释:

val asc = list.sorted()

sorted() 方法按升序对列表进行排序。

val desc = list.sortedDescending()

sortedDescending() 方法按降序对列表进行排序。

Contains() 和 containsAll() 函数——

此方法检查列表中是否存在元素。

Java

fun main(args: Array)
{
    val list = listOf(8, 4, 7, 1, 2, 3, 0, 5, 6 )
 
    val res = list.contains(0)
 
    if (res)
        println("The list contains 0")
    else
        println("The list does not contain 0")
 
    val result = list.containsAll(listOf(3, -1))
 
    if (result)
        println("The list contains 3 and -1")
    else
        println("The list does not contain 3 and -1")
}

输出:

The list contains 0
The list does not contain 3 and -1

解释:

val res = list.contains(0)

检查列表是否包含 0 并返回存储在 res 中的 true 或 false(她的 true)。

val result = list.containsAll(listOf(3, -1))

检查列表是否包含 3 和 -1。