val listString= listOf<String>("one","two","one")
println(listString)
输出:[one, two, one]
// set集合去重
val setString= setOf<String>("one","two","one")
println(setString)
输出:[one, two]
2、可变集合
kotlin复制代码
val numbers= mutableListOf<Int>(1,2,3,4)
numbers.add(6)
numbers.removeAt(1)
numbers[0]=10
println(numbers)
输出:[10, 3, 4, 6]
// set集合自动过滤重复元素
val hello= mutableSetOf<String>("h","e","l","l","o")
hello.remove("o")
println(hello)
输出:[h, e, l]
//集合的加减操作:
hello+=setOf("0","w","o","r","l","d")
输出: [h, e, l, 0, w, o, r, d]
3、Map集合
kotlin复制代码
val numberMap= mapOf<String,Int>("a" to 1,"b" to 2,"c" to 3,"d" to 4)
println("keys:${numberMap.keys}")
println("values:${numberMap.values}")
if ("a" in numberMap.keys){}
if (2 in numberMap.values){}
输出:
keys:[a, b, c, d]
values:[1, 2, 3, 4]