Tag(Kotlin 筆記)
Kotlin 中的 List、Map、Set 只能讀取,是無法進行修改,若需要可讀可寫,需使用 MutableList、MutableMap、MutableSet。
Set 唯讀 / MutableSet 可新增,不可修改
特性:
MutableSet 可新增,不可修改
//Set 初始化
val set: Set<String> = setOf("小明", "小美", "小王")
// MutableSet 初始化
var mutableSet: MutableSet<String> = mutableSetOf("小明", "小美", "小王")
List / MutableList
特性:
MutableList 新增、刪除、修改
//List
val list: List<String> = listOf("iPhone", "Android", "ios")
// MutableList
var mutableList: MutableList<String> = mutableListOf("iPhone", "Android", "ios")
特性:
MutableMap 新增、刪除、修改
//mapOf 初始化 [key to value]
val map: Map<String, String> = mapOf(
"iPhone" to "1" ,
"Android" to "2",
"ios" to "3")
//MutableMapOf 初始化 Pair(key, value)
var mutableMap: MutableMap<String, String> = mutableMapOf(
Pair( "iPhone","1"),
Pair( "Android","2"),
Pair( "ios","3")
)