一、MutableList 概述
MutableList 是 Kotlin 中可变的列表接口,它继承自 List 接口并添加了修改列表内容的方法
MutableList 允许添加、删除、更新元素
二、创建 MutableList
1、基础创建
- 使用 mutableListOf 函数
// 创建一个 MutableList,包含 4 个元素
val nums = mutableListOf(1, 2, 3, 4)
println("nums: $nums")
# 输出结果
nums: [1, 2, 3, 4]
- 使用 MutableList 函数
// 创建一个 MutableList,包含 5 个元素,基于索引计算每个元素的值
val nums = MutableList(5) { it * 2 }
println("nums: $nums")
# 输出结果
nums: [0, 2, 4, 6, 8]
- 使用 ArrayList 构造函数
val nums = ArrayList<Int>()
nums.add(1)
nums.add(2)
nums.add(3)
println("nums: $nums")
# 输出结果
nums: [1, 2, 3]
2、创建空 MutableList
- 使用 mutableListOf 函数
val emptyList = mutableListOf<String>()
println("emptyList: $emptyList")
# 输出结果
emptyList: []
- 使用 MutableList 函数
val emptyList = MutableList(0) { it }
println("emptyList: $emptyList")
# 输出结果
emptyList: []
- 使用 ArrayList 构造函数
// 指定初始容量为 5
val nums1 = ArrayList<Int>(5)
println("nums1: $nums1")
val nums2 = ArrayList<Int>()
println("nums2: $nums2")
# 输出结果
nums1: []
nums2: []
三、MutableList 添加元素
- 添加单个元素
val fruits = mutableListOf("Apple", "Banana")
println("fruits: $fruits")
// 在末尾添加元素
fruits.add("Orange")
println("fruits: $fruits")
// 在指定位置添加元素
fruits.add(1, "Grape")
println("fruits: $fruits")
# 输出结果
fruits: [Apple, Banana]
fruits: [Apple, Banana, Orange]
fruits: [Apple, Grape, Banana, Orange]
- 添加多个元素
val fruits = mutableListOf("Apple", "Banana")
println("fruits: $fruits")
// 在末尾添加多个元素
fruits.addAll(listOf("Mango", "Peach"))
println("fruits: $fruits")
// 在指定位置添加多个元素
fruits.addAll(2, listOf("Lemon", "Kiwi"))
println("fruits: $fruits")
# 输出结果
fruits: [Apple, Banana]
fruits: [Apple, Banana, Mango, Peach]
fruits: [Apple, Banana, Lemon, Kiwi, Mango, Peach]
四、MutableList 删除元素
- 按值删除元素
val nums = mutableListOf(1, 2, 3, 4, 5, 2)
println("nums: $nums")
nums.remove(2)
println("nums: $nums")
# 输出结果
nums: [1, 2, 3, 4, 5, 2]
nums: [1, 3, 4, 5, 2]
- 按索引删除元素
val nums = mutableListOf(1, 2, 3, 4, 5, 2)
println("nums: $nums")
nums.removeAt(2)
println("nums: $nums")
# 输出结果
nums: [1, 2, 3, 4, 5, 2]
nums: [1, 2, 4, 5, 2]
- 删除所有匹配元素
val nums = mutableListOf(1, 2, 3, 4, 5, 2)
println("nums: $nums")
nums.removeAll(listOf(4, 2))
println("nums: $nums")
# 输出结果
nums: [1, 2, 3, 4, 5, 2]
nums: [1, 3, 5]
- 删除所有元素
val nums = mutableListOf(1, 2, 3, 4, 5, 2)
println("nums: $nums")
nums.clear()
println("nums: $nums")
# 输出结果
nums: [1, 2, 3, 4, 5, 2]
nums: []
- 保留指定元素,删除其余元素
val letters = mutableListOf('a', 'b', 'c', 'd')
println("letters: $letters")
letters.retainAll(listOf('a', 'c'))
println("letters: $letters")
# 输出结果
letters: [a, b, c, d]
letters: [a, c]
五、MutableList 访问元素
1、基础访问
- 使用
[]
操作符与 get 方法
val colors = mutableListOf("红", "绿", "蓝")
val first = colors[0]
val second = colors.get(1)
println("first: $first")
println("second: $second")
# 输出结果
first: 红
second: 绿
- 它们都会抛出索引越界异常
val colors = mutableListOf("红", "绿", "蓝")
val result = colors[10]
println("result: $result")
# 输出结果
java.lang.IndexOutOfBoundsException: Index 10 out of bounds for length 3
val colors = mutableListOf("红", "绿", "蓝")
val result = colors.get(10)
println("result: $result")
# 输出结果
java.lang.IndexOutOfBoundsException: Index 10 out of bounds for length 3
2、安全访问
(1)基本介绍
方法 | 说明 |
---|---|
firstOrNull | 返回 List 的第一个元素,如果 List 为空则返回 null |
lastOrNull | 返回 List 的最后一个元素,如果 List 为空则返回 null |
getOrNull | 返回指定索引的元素,如果索引超出范围则返回 null |
getOrElse | 返回指定索引的元素,如果索引超出范围则返回默认值 |
(2)演示
val colors = mutableListOf("红", "绿", "蓝")
val result1 = colors.firstOrNull()
val result2 = colors.lastOrNull()
val result3 = colors.getOrNull(5)
val result4 = colors.getOrElse(5) { "默认颜色" }
println("result1: $result1")
println("result2: $result2")
println("result3: $result3")
println("result4: $result4")
# 输出结果
result1: 红
result2: 蓝
result3: null
result4: 默认颜色
val colors = mutableListOf<String>()
val result1 = colors.firstOrNull()
val result2 = colors.lastOrNull()
val result3 = colors.getOrNull(5)
val result4 = colors.getOrElse(5) { "默认颜色" }
println("result1: $result1")
println("result2: $result2")
println("result3: $result3")
println("result4: $result4")
# 输出结果
result1: null
result2: null
result3: null
result4: 默认颜色
3、条件检索
val colors = mutableListOf("red", "green", "blue", "yellow")
// 找到第一个符合条件的
val result1 = colors.first { it.length >= 5 }
// 找到最后一个符合条件的
val result2 = colors.last { it.length == 4 }
println("result1: $result1")
println("result2: $result2")
# 输出结果
result1: green
result2: blue
六、MutableList 修改元素
- 使用
[]
操作符
val items = mutableListOf("one", "two", "three")
println("items: $items")
items[1] = "2"
println("items: $items")
# 输出结果
items: [one, two, three]
items: [one, 2, three]
- 使用 set 方法
val items = mutableListOf("one", "two", "three")
println("items: $items")
items.set(2, "3")
println("items: $items")
# 输出结果
items: [one, two, three]
items: [one, two, 3]
七、MutableList 遍历元素
1、for 循环(推荐)
- 遍历元素
val items = mutableListOf("one", "two", "three")
for (item in items) {
println(item)
}
# 输出结果
one
two
three
- 遍历索引
val items = mutableListOf("one", "two", "three")
for (i in items.indices) {
println("Index $i: ${items[i]}")
}
# 输出结果
Index 0: one
Index 1: two
Index 2: three
2、使用 forEach 方法(推荐)
val items = mutableListOf("one", "two", "three")
items.forEach { println(it) }
# 输出结果
one
two
three
val items = mutableListOf("one", "two", "three")
items.forEach { item ->
println(item)
}
# 输出结果
one
two
three
3、使用 forEachIndexed 方法(推荐)
val items = mutableListOf("one", "two", "three")
items.forEachIndexed { index, item ->
println("Index $index: $item")
}
# 输出结果
Index 0: one
Index 1: two
Index 2: three
4、使用迭代器
val items = mutableListOf("one", "two", "three")
val iterator = items.iterator()
while (iterator.hasNext()) {
println(iterator.next())
}
# 输出结果
one
two
three
5、使用 withIndex 方法
val colors = mutableListOf("红", "绿", "蓝")
for ((index, value) in colors.withIndex()) {
println("$index: $value")
}
# 输出结果
0: 红
1: 绿
2: 蓝
6、使用 List.size
+ get 方法
val fruitList = mutableListOf("Apple", "Banana", "Orange")
for (i in 0 until fruitList.size) {
println(fruitList.get(i))
}
# 输出结果
Apple
Banana
Orange