练习题:Kotlin小玩意

kotlin 计算list数据个数转map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fun main(args: Array<String>) {

val data = 1..100
val preList = arrayListOf<Int>()
var resMap = mutableMapOf<Int,Int>(1 to 0,2 to 0,3 to 0,4 to 0, 5 to 0)
for(item in data){
preList.add( (1..5).random())
}

preList.forEach{
with(resMap){
this[it] = this[it]!! + 1
}
}
println(resMap)

}