集合
在進行程式設計時,能夠將資料分組到各種結構中以便後續處理是非常有用的。Kotlin 提供的集合(collection)正是用於此目的。
Kotlin 提供以下集合來為項目進行分組:
| 集合型別 | 描述 |
|---|---|
| Lists | 有序的項目集合 |
| Sets | 唯一的無序項目集合 |
| Maps | 鍵值組(key-value pair)的集合,其中鍵是唯一的,且每個鍵僅對應到一個值 |
每種集合型別都可以是可變的(mutable)或唯讀的(read-only)。
List
List 會按照項目被新增的順序來儲存它們,並允許出現重複項目。
若要建立唯讀清單(List),請使用 listOf() 函式。
若要建立可變清單(MutableList), 請使用 mutableListOf() 函式。
建立清單時,Kotlin 可以推論儲存項目的型別。若要明確宣告型別,可以在清單宣告後的尖括號 <> 內加上型別:
fun main() {
// Read only list
val readOnlyShapes = listOf("triangle", "square", "circle")
println(readOnlyShapes)
// [triangle, square, circle]
// Mutable list with explicit type declaration
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
println(shapes)
// [triangle, square, circle]
}為了防止意外修改,你可以將可變清單指派給 List,以建立唯讀檢視:
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
val shapesLocked: List<String> = shapes這也稱為型別轉換(casting)。
由於清單是有序的,若要存取清單中的項目,請使用索引存取運算子 []:
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
}若要取得清單中的第一個或最後一個項目,請分別使用 .first() 與 .last() 函式:
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println("The first item in the list is: ${readOnlyShapes.first()}")
// The first item in the list is: triangle
}.first() 與 .last() 函式都是擴充函式(extension function)的範例。若要在物件上呼叫擴充函式,請在物件名稱後方加上句號 .,接著寫上函式名稱。
擴充函式在中階教學中有詳細介紹。 目前你只需要知道如何呼叫它們即可。
若要取得清單中的項目數量,請使用 .count() 函式:
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println("This list has ${readOnlyShapes.count()} items")
// This list has 3 items
}若要檢查某個項目是否存在於清單中,請使用 in 運算子:
fun main() {
val readOnlyShapes = listOf("triangle", "square", "circle")
println("circle" in readOnlyShapes)
// true
}若要從可變清單中新增或移除項目,請分別使用 .add() 與 .remove() 函式:
fun main() {
val shapes: MutableList<String> = mutableListOf("triangle", "square", "circle")
// Add "pentagon" to the list
shapes.add("pentagon")
println(shapes)
// [triangle, square, circle, pentagon]
// Remove the first "pentagon" from the list
shapes.remove("pentagon")
println(shapes)
// [triangle, square, circle]
}Set
清單是有序且允許重複項目的,而 Set 則是無序且僅儲存唯一項目。
若要建立唯讀 Set(Set),請使用 setOf() 函式。
若要建立可變 Set(MutableSet), 請使用 mutableSetOf() 函式。
建立 Set 時,Kotlin 可以推論儲存項目的型別。若要明確宣告型別,可以在 Set 宣告後的尖括號 <> 內加上型別:
fun main() {
// Read-only set
val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
// Mutable set with explicit type declaration
val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
println(readOnlyFruit)
// [apple, banana, cherry]
}你可以從前面的範例中看到,因為 Set 只包含唯一元素,所以重複的 "cherry" 項目會被捨棄。
為了防止意外修改,你可以將可變 Set 指派給 Set,以建立唯讀檢視:
val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
val fruitLocked: Set<String> = fruit由於 Set 是無序的,因此你無法存取特定索引處的項目。
若要取得 Set 中的項目數量,請使用 .count() 函式:
fun main() {
val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
println("This set has ${readOnlyFruit.count()} items")
// This set has 3 items
}若要檢查某個項目是否存在於 Set 中,請使用 in 運算子:
fun main() {
val readOnlyFruit = setOf("apple", "banana", "cherry", "cherry")
println("banana" in readOnlyFruit)
// true
}若要從可變 Set 中新增或移除項目,請分別使用 .add() 與 .remove() 函式:
fun main() {
val fruit: MutableSet<String> = mutableSetOf("apple", "banana", "cherry", "cherry")
fruit.add("dragonfruit") // Add "dragonfruit" to the set
println(fruit) // [apple, banana, cherry, dragonfruit]
fruit.remove("dragonfruit") // Remove "dragonfruit" from the set
println(fruit) // [apple, banana, cherry]
}Map
Map 以鍵值組(key-value pair)的形式儲存項目。你可以透過參照鍵來存取對應的值。你可以把 Map 想像成一張菜單: 透過尋找你想吃的餐點(鍵),即可查到對應的價格(值)。如果你想要查詢某個值,又不想像在清單中那樣使用數字索引時,Map 會非常實用。
- Map 中的每個鍵都必須是唯一的,這樣 Kotlin 才能識別你想取得哪一個值。
- Map 中可以有重複的值。
若要建立唯讀 Map(Map),請使用 mapOf() 函式。
若要建立可變 Map(MutableMap), 請使用 mutableMapOf() 函式。
建立 Map 時,Kotlin 可以推論儲存項目的型別。若要明確宣告型別,可以在 Map 宣告後的尖括號 <> 內加上鍵與值的型別。 例如:MutableMap<String, Int>。其中鍵的型別為 String,值的型別為 Int。
建立 Map 最簡單的方式是在每個鍵與其相關聯的值之間使用 to:
fun main() {
// Read-only map
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu)
// {apple=100, kiwi=190, orange=100}
// Mutable map with explicit type declaration
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(juiceMenu)
// {apple=100, kiwi=190, orange=100}
}為了防止意外修改,你可以將可變 Map 指派給 Map,以建立唯讀檢視:
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
val juiceMenuLocked: Map<String, Int> = juiceMenu若要存取 Map 中的值,請使用索引存取運算子 [] 並傳入其鍵:
fun main() {
// Read-only map
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println("The value of apple juice is: ${readOnlyJuiceMenu["apple"]}")
// The value of apple juice is: 100
}如果你嘗試使用 Map 中不存在的鍵來存取鍵值組,將會得到 null 值:
fun main() {
// Read-only map
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println("The value of pineapple juice is: ${readOnlyJuiceMenu["pineapple"]}")
// The value of pineapple juice is: null
}本教學後續將在空值安全章節中詳細說明 null 值。
你也可以使用索引存取運算子 [] 來向可變 Map 新增項目:
fun main() {
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
juiceMenu["coconut"] = 150 // Add key "coconut" with value 150 to the map
println(juiceMenu)
// {apple=100, kiwi=190, orange=100, coconut=150}
}若要從可變 Map 中移除項目,請使用 .remove() 函式:
fun main() {
val juiceMenu: MutableMap<String, Int> = mutableMapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
juiceMenu.remove("orange") // Remove key "orange" from the map
println(juiceMenu)
// {apple=100, kiwi=190}
}若要取得 Map 中的項目數量,請使用 .count() 函式:
fun main() {
// Read-only map
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println("This map has ${readOnlyJuiceMenu.count()} key-value pairs")
// This map has 3 key-value pairs
}若要檢查 Map 中是否已包含特定鍵,請使用 .containsKey() 函式:
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println(readOnlyJuiceMenu.containsKey("kiwi"))
// true
}若要取得 Map 的鍵或值的集合,請分別使用 keys 與 values 屬性:
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]
}keys 與 values 都是物件屬性(property)的範例。若要存取物件的屬性,請在物件名稱後方加上句號 .,接著寫上屬性名稱。
屬性將在類別章節中進行更詳細的討論。 在教學的這個階段,你只需要知道如何存取它們即可。
若要檢查某個鍵或值是否存在於 Map 中,請使用 in 運算子:
fun main() {
val readOnlyJuiceMenu = mapOf("apple" to 100, "kiwi" to 190, "orange" to 100)
println("orange" in readOnlyJuiceMenu.keys)
// true
// Alternatively, you don't need to use the keys property
println("orange" in readOnlyJuiceMenu)
// true
println(200 in readOnlyJuiceMenu.values)
// false
}若要深入了解集合的更多操作,請參閱集合。
現在你已經了解了基本型別以及如何管理集合,接下來是時候探索可在程式中使用的控制流程了。
練習
計算兩個清單中的項目總數
你有一個「綠色」數字清單和一個「紅色」數字清單。請補全程式碼以印出共有多少個數字。
// Keep these two lists as they are. Use them in main().
val greenNumbers = listOf(1, 4, 23)
val redNumbers = listOf(17, 2)
fun main() {
// Write your code here
}Example solution {...}
val greenNumbers = listOf(1, 4, 23)
val redNumbers = listOf(17, 2)
fun main() {
val totalCount = greenNumbers.count() + redNumbers.count()
println(totalCount)
}檢查是否支援請求的通訊協定
你的伺服器有一組支援的通訊協定集合。使用者請求使用特定協定。請補全程式以檢查是否支援所請求的通訊協定(isSupported 必須為布林值)。
fun main() {
val SUPPORTED = setOf("HTTP", "HTTPS", "FTP")
val requested = "smtp"
val isSupported = TODO() // Replace the TODO() function
println("Support for $requested: $isSupported")
}提示
.uppercase() 函式來協助完成此操作。 Example solution {...}
fun main() {
val SUPPORTED = setOf("HTTP", "HTTPS", "FTP")
val requested = "smtp"
val isSupported = requested.uppercase() in SUPPORTED
println("Support for $requested: $isSupported")
}使用 Map 拼寫數字
定義一個將整數 1 到 3 與其對應英文拼寫關聯起來的 Map。使用此 Map 拼寫出指定的數字。
// Replace the empty map. Keep the variable name as number2word.
val number2word: Map<Int, String> = emptyMap()
fun main() {
val n = 2
println("$n is spelled as '${number2word[n]}'")
}Example solution {...}
val number2word = mapOf(1 to "one", 2 to "two", 3 to "three")
fun main() {
val n = 2
println("$n is spelled as '${number2word[n]}'")
}