Kotlin 列表怎么创建和管理?

文章导读
Previous Quiz Next Kotlin 列表是有序的项目集合。Kotlin 列表可以是可变的(mutableListOf)或只读的(listOf)。可以使用索引访问列表的元素。Kotlin 可变或不可变列表可以包含重复元素。
📋 目录
  1. 创建 Kotlin 列表
  2. 遍历 Kotlin 列表
  3. Kotlin 列表的大小
  4. "in" 操作符
  5. contain() 方法
  6. isEmpty() 方法
  7. indexOf() 方法
  8. get() 方法
  9. List Addition
  10. List Subtraction
A A

Kotlin - 列表



Previous
Quiz
Next

Kotlin 列表是有序的项目集合。Kotlin 列表可以是可变的(mutableListOf)或只读的(listOf)。可以使用索引访问列表的元素。Kotlin 可变或不可变列表可以包含重复元素。

创建 Kotlin 列表

对于列表创建,使用标准库函数 listOf() 创建只读列表,mutableListOf() 创建可变列表。

为了防止意外修改,可以通过将可变列表转换为 List 来获取只读视图。

示例

fun main() {
    val theList = listOf("one", "two", "three", "four")
    println(theList)
    
    val theMutableList = mutableListOf("one", "two", "three", "four")
    println(theMutableList)
}

运行上述 Kotlin 程序,将生成以下输出:

[one, two, three, four]
[one, two, three, four]

遍历 Kotlin 列表

有多种方式遍历 Kotlin 列表。我们逐一学习:

使用 toString() 函数

fun main() {
    val theList = listOf("one", "two", "three", "four")
    println(theList.toString())
}

运行上述 Kotlin 程序,将生成以下输出:

[one, two, three, four]

使用 Iterator

fun main() {
    val theList = listOf("one", "two", "three", "four")
    
    val itr = theList.listIterator() 
    while (itr.hasNext()) {
        println(itr.next())
    }
}

运行上述 Kotlin 程序,将生成以下输出:

one
two
three
four

使用 for 循环

fun main() {
   val theList = listOf("one", "two", "three", "four")
    
   for (i in theList.indices) {
      println(theList[i])
   }
}

运行上述 Kotlin 程序,将生成以下输出:

one
two
three
four

使用 forEach

fun main() {
   val theList = listOf("one", "two", "three", "four")
    
   theList.forEach { println(it) }
}

运行上述 Kotlin 程序,将生成以下输出:

one
two
three
four
注意 - 此处 it 类似于 Java 中的 this 操作符。

Kotlin 列表的大小

可以使用 size 属性获取列表中元素的总数:

fun main() {
    val theList = listOf("one", "two", null, "four", "five")
    
    println("Size of the list " + theList.size)
}

运行上述 Kotlin 程序,将生成以下输出:

Size of the list 5

"in" 操作符

in 操作符可用于检查列表中是否存在某个元素。

示例

fun main() {
   val theList = listOf("one", "two", "three", "four")
    
   if("two" in theList){
      println(true)
   }else{
      println(false)
   }
}

运行上述 Kotlin 程序,将生成以下输出:

true

contain() 方法

contain() 方法也可用于检查列表中是否存在某个元素。

示例

fun main() {
   val theList = listOf("one", "two", "three", "four")

   if(theList.contains("two")){
      println(true)
   }else{
      println(false)
   }
    
}

运行上述 Kotlin 程序,将生成以下输出:

true

isEmpty() 方法

isEmpty() 方法如果集合为空(不包含任何元素)则返回 true,否则返回 false

示例

fun main() {
   val theList = listOf("one", "two", "three", "four")
    
   if(theList.isEmpty()){
      println(true)
   }else{
      println(false)
   }
}

运行上述 Kotlin 程序,将生成以下输出:

false

indexOf() 方法

indexOf() 方法返回列表中指定元素第一次出现的位置索引,如果列表中不包含该元素,则返回 -1。

示例

fun main() {
   val theList = listOf("one", "two", "three", "four")
    
   println("Index of 'two' :  " + theList.indexOf("two"))
}

运行上述 Kotlin 程序,将生成以下输出:

Index of 'two' :  1

get() 方法

get() 方法可用于获取列表中指定索引位置的元素。第一个元素的索引为零。

示例

fun main() {
   val theList = listOf("one", "two", "three", "four")

   println("Element at 3rd position " + theList.get(2))
}

运行上述 Kotlin 程序,将生成以下输出:

Element at 3rd position three

List Addition

我们可以使用 + 操作符将两个或多个列表合并为一个列表。这会将第二个列表添加到第一个列表中,即使有重复元素也会被添加。

示例

fun main() {
    val firstList = listOf("one", "two", "three")
    val secondList = listOf("four", "five", "six")
    val resultList = firstList + secondList
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[one, two, three, four, five, six]

List Subtraction

我们可以使用 - 操作符从一个列表中减去另一个列表。此操作将从第一个列表中移除公共元素,并返回结果。

示例

fun main() {
    val firstList = listOf("one", "two", "three")
    val secondList = listOf("one", "five", "six")
    val resultList = firstList - secondList
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[two, three]

切片 List

我们可以使用 slice() 方法通过元素索引范围从给定列表中获取子列表。

示例

fun main() {
    val theList = listOf("one", "two", "three", "four", "five")
    val resultList = theList.slice( 2..4)
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[three, four, five]

Removing null a List

我们可以使用 filterNotNull() 方法从 Kotlin 列表中移除 null 元素。

fun main() {
    val theList = listOf("one", "two", null, "four", "five")
    val resultList = theList.filterNotNull()
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[one, two, four, five]

过滤元素

我们可以使用 filter() 方法过滤出与给定 predicate 匹配的元素。

fun main() {
    val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
    val resultList = theList.filter{ it > 30}
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[31, 40, 50]

丢弃前 N 个元素

我们可以使用 drop() 方法从列表中丢弃前 N 个元素。

fun main() {
    val theList = listOf(10, 20, 30, 31, 40, 50, -1, 0)
    val resultList = theList.drop(3)
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[31, 40, 50, -1, 0]

分组 List 元素

我们可以使用 groupBy() 方法对与给定 predicate 匹配的元素进行分组。

fun main() {
    val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
    val resultList = theList.groupBy{ it % 3}
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

{1=[10, 31, 40], 0=[12, 30, 9, -3, 0]}

映射列表元素

我们可以使用 map() 方法通过提供的 function 将所有元素进行映射。

fun main() {
    val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
    val resultList = theList.map{ it / 3 }
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[3, 4, 10, 10, 13, 3, -1, 0]

分块列表元素

我们可以使用 chunked() 方法从列表中创建给定大小的块。最后一个块的元素数量可能不等于块大小,这取决于列表中元素的总数。

fun main() {
    val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
    val resultList = theList.chunked(3)
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[[10, 12, 30], [31, 40, 9], [-3, 0]]

窗口化列表元素

我们可以使用 windowed() 方法,通过在元素集合上移动给定大小的滑动窗口,来创建元素范围的列表。

fun main() {
    val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
    val resultList = theList.windowed(3)
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[[10, 12, 30], [12, 30, 31], [30, 31, 40], [31, 40, 9], [40, 9, -3], [9, -3, 0]]

默认情况下,滑动窗口每次移动一步,但我们可以通过传递自定义步长值来改变这一点:

fun main() {
    val theList = listOf(10, 12, 30, 31, 40, 9, -3, 0)
    val resultList = theList.windowed(3, 3)
    
    println(resultList)
}

运行上述 Kotlin 程序,将生成以下输出:

[[10, 12, 30], [31, 40, 9]]

Kotlin 可变 List

我们可以使用 mutableListOf() 创建可变列表,之后可以使用 add() 在同一列表中添加更多元素,并使用 remove() 方法从列表中移除元素。

fun main() {
    val theList = mutableSetOf(10, 20, 30)

    theList.add(40)
    theList.add(50)
    println(theList)

    theList.remove(10)
    theList.remove(30)
    println(theList)
}

运行上述 Kotlin 程序,将生成以下输出:

[10, 20, 30, 40, 50]
[20, 40, 50]

Kotlin List 参考

在 Kotlin 中,List(一个有序的元素集合,允许重复)是一种 collection,其中 collection 是一组相关对象的集合。它提供了一种高效管理和操作数据的方式。

属性

以下是在 Kotlin List collection 中定义的属性 −

序号 属性 & 描述
1 size

返回列表的大小。

函数

以下是在 Kotlin List 集合中定义的函数 −

序号 函数 & 描述
1 containsAll()

检查列表是否包含所有指定的元素。

2 contains()

检查列表是否包含指定的元素。

3 filterNot()

返回不符合谓词的元素。

4 filter()

返回符合谓词的元素。

5 flatMap()

将每个元素转换为集合并将其展平。

6 forEach()

对每个元素执行指定的操作。

7 get()

返回指定索引处的元素。

8 groupBy()

根据键对元素进行分组。

9 isEmpty()

检查列表是否为空。

10 isNotEmpty()

检查列表是否非空。

11 iterator()

返回列表的迭代器。

12 joinToString()

将列表转换为字符串表示。

13 map()

转换每个元素并返回新列表。

14 random()

从集合中返回一个随机元素。

15 toMap()

将列表转换为 map 集合类型。

16 toSet()

将列表转换为 set 集合类型。

测试时间 (面试 & 考试准备)

问题 1 - 我们可以将 mutable Kotlin 列表变为 immutable 吗?

A - 是

B - 否

答案:A

解释

是的,我们可以通过将 mutable 列表转换为 List 来使其变为 immutable。

问题 2 - 我们可以使用 + 操作符将两个或多个列表相加并创建一个单一列表:

A - 真

B - 假

答案:A

解释

是的,我们可以对两个 Kotlin 列表进行相加或相减操作,并生成第三个列表。

问题 3 - Kotlin 列表的 get() 方法的作用是什么?

A - 从给定索引获取列表元素。

B - 返回列表的所有值

C - 列表不支持此方法

D - 以上皆非

答案:A

解释

get() 方法用于从给定索引获取列表元素。