集合有助于数据分组,方便后续操作
集合类型 | 说明 |
---|---|
Lists | 有序的可重复的集合 |
Sets | 无序的不可重复的集合 |
Maps | 键值对映射集合,键唯一,且一个键只能映射到一个值 |
每个集合类型都可以是可变的或者只读的
List
按照添加的顺序存储内容,并允许重复
存储的内容通常称作
元素
创建只读List
使用listOf()
方法
创建可变List
使用mutableListOf()
方法
在创建列表时,Kotlin可以推断存储项的类型。可以在列表声明后的尖括号<>中添加类型来显式声明类型
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println(readOnlyShapes) // [triangle, square, circle]
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
println(shapes) // [triangle, square, circle]
shapes[2] = "circle2"
// shapes[3] = "circle3" // Index 3 out of bounds for length 3
println(shapes) // [triangle, square, circle2]
}
操作
MutableList
索引不能超过初始长度
为了防止不必要的修改,你可以通过将可变列表赋值给List来获得它们的只读视图
fun main() {
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
val shapesLocked: List<String> = shapes
}
这种方式通常叫做铸造(casting)
查看List
方法
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println("The first item in the list is: ${readOnlyShapes[0]}") // The first item in the list is: triangle
println("The first item in the list is: ${readOnlyShapes.first()}") // The first item in the list is: triangle
println("The last item in the list is: ${readOnlyShapes.last()}") // The last item in the list is: circle
}
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println("This list has ${readOnlyShapes.count()} items") // This list has 3 items
}
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println("circle" in readOnlyShapes) // true
}
fun main() {
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
// 添加 "pentagon"
shapes.add("pentagon")
println(shapes) // [triangle, square, circle, pentagon]
// 删除第一个 "pentagon"
shapes.remove("pentagon")
println(shapes) // [triangle, square, circle]
}
Set
集合中存储的数据无序并且不能重复
创建只读Set
使用setOf()
方法
创建可变Set
使用MutableList()
方法
fun main() {
val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
}
因为Set
为无序集合,所以不能通过索引获取集合元素
fun main() {
val set = setOf("apple", "banana", "cherry", "cherry")
println("The first item in the set is: ${set.first()}") // The first item in the set is: apple
println("The last item in the set is: ${set.last()}") // The last item in the set is: cherry
}
Map
以键值对的形式存储数据。你可以通过引用键来访问值
键是唯一的,如果插入重复键则会覆盖之前的值
要创建只读Map
,使用mapOf()
函数
要创建可变地图MutableMap
,使用mutableMapOf()
函数
在创建
Map
时,kotlin
可以推断出存储的元素类型。要显式声明类型,可以在Map
声明后的尖括号<>中添加键和值的类型。例如:MutableMap
。键的类型为String
,值的类型为Int
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu) // {apple=100, kiwi=190, orange=100}
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(juiceMenu) // {apple=100, kiwi=190, orange=100}
}
使用键获取值
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu["apple"]) // 100
}
获取所有的键或者值
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu.keys) // [apple, kiwi, orange]
println(readOnlyJuiceMenu.values) // [100, 190, 100]
}
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu.count()) // 3
}
fun main() {
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
juiceMenu.put("coconut", 150) // 添加键"coconut"和值 150
println(juiceMenu) // {apple=100, kiwi=190, orange=100, coconut=150}
juiceMenu.put("apple", 200) // 修改"coconut"的值成 200
println(juiceMenu) // {apple=200, kiwi=190, orange=100, coconut=150}
juiceMenu.remove("orange") // 删除"orange"
println(juiceMenu) // {apple=200, kiwi=190, coconut=150}
}
是否包含某个键
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu.containsKey("kiwi")) // true
println("orange" in readOnlyJuiceMenu.keys) // true
}
是否包含某个值
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(200 in readOnlyJuiceMenu.values) // false
}