集合有助于數據分組,方便后續操作
集合類型 | 說明 |
---|---|
Lists | 有序的可重復的集合 |
Sets | 無序的不可重復的集合 |
Maps | 鍵值對映射集合,鍵唯一,且一個鍵只能映射到一個值 |
每個集合類型都可以是可變的或者只讀的
List
List
按照添加的順序存儲內容,并允許重復
存儲的內容通常稱作
元素
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 3println(shapes) // [triangle, square, circle2]
}
操作
MutableList
索引不能超過初始長度
為了防止不必要的修改,你可以通過將可變列表賦值給List來獲得它們的只讀視圖
fun main() {val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")val shapesLocked: List<String> = shapes
}
這種方式通常叫做鑄造(casting)
List常用方法
查看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: triangleprintln("The first item in the list is: ${readOnlyShapes.first()}") // The first item in the list is: triangleprintln("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
集合中存儲的數據無序并且不能重復
Set類型
創建只讀Set
使用setOf()
方法
創建可變Set
使用MutableList()
方法
fun main() {val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
}
Set常用方法
獲取元素
因為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: appleprintln("The last item in the set is: ${set.last()}") // The last item in the set is: cherry
}
Map
Map
以鍵值對的形式存儲數據。你可以通過引用鍵來訪問值
鍵是唯一的,如果插入重復鍵則會覆蓋之前的值
Map類型
要創建只讀Map
,使用mapOf()
函數
要創建可變地圖MutableMap
,使用mutableMapOf()
函數
在創建
Map
時,kotlin
可以推斷出存儲的元素類型。要顯式聲明類型,可以在Map
聲明后的尖括號<>中添加鍵和值的類型。例如:MutableMap<String, Int>
。鍵的類型為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"和值 150println(juiceMenu) // {apple=100, kiwi=190, orange=100, coconut=150}juiceMenu.put("apple", 200) // 修改"coconut"的值成 200println(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")) // trueprintln("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
}