欢迎来到风的博客
Kotlin动态
大小固定,元素类型不可变
1 2 3 4 5 6
| fun main(array: Array<String>) { var arrays = arrayOf("java","c","Kotlin") for (a in arrays ){ println(a) } }
|
创建默认值数组
1
| var arrays = Array(20, { "Kotlin" })
|
创建自增数组
1
| var count = Array(10, { i -> i + 1 })
|
数组常用方法
1 2 3 4
| count.size count.isEmpty()
|
过滤重复数组
1 2 3
| println(arrays.distinct()) println(arrays.toSet())
|
提示:hashSetOf自身有去重
1 2 3 4 5
| var has = hashSetOf("java", "Kotlin", "Kotlin", "Kotlin", "Kotlin", "Kotlin") for (h in has) { println(h) }
|
切割数组
1
| val Intercept=arrays.sliceArray(2..4)
|
大小可变,元素类型不可变
1 2 3 4 5
| var mutableListOfs = mutableListOf("java","c","C++","java","Visual Basic"," Scala","Kotlin","java","c","Kotlin") mutableListOfs.add("Swift") mutableListOfs.addAll(arrays)
|
无序不重复
1 2 3 4 5 6 7 8 9 10
| val mainLine = setOf("Java", "Swift", "C++", "PHP", "Go", "Python") val twoLine = setOf("C++", "C", "Ruby", "Objective-C") val threeLine = setOf("C++", "Scala", "Dart") println(mainLine.count()) println(mainLine.isEmpty()) println(mainLine.contains("Swift")) println(mainLine.containsAll(twoLine))
|
转换数组
1 2 3 4
| val array = mainLine.toTypedArray() for (a in array) { println(a) }
|
集合运算
1 2 3 4 5 6
| val meetLines = mainLine.intersect(twoLine) println(meetLines) val diffLine = mainLine.subtract(twoLine) println(diffLine) println(mainLine.union(twoLine)) println(mainLine.minus(twoLine))
|
Map使用,没有顺序位置会改变
1 2 3 4 5 6 7 8
| val mapList = mapOf<String, String>(Pair("a", "1"), Pair("b", "1"), Pair("c", "1"), Pair("d", "1")) println("a".toLowerCase()) println("a".toUpperCase()) println(mapList.getOrDefault("A".toLowerCase(), "不存在")) mapList.keys mapList.values
|
转换为可变
1 2 3 4 5 6 7 8 9 10 11
| val tmapList = mapList.toMutableMap() tmapList["a"] = "8648" tmapList.put("e", "156156") tmapList.remove("a") for (t in tmapList) { println(t) }
|
Demo下载地址:https://github.com/Ysrfengnull/kt.git
- Search in QQ group 200409033