如果您熟悉Scala的集合库,您会发现Kotlin跟Scala集合类库的相似之处。...创建一个可变集合:
>>> val mutableList = mutableListOf(1,2,3)
>>> mutableList
[1, 2, 3]
向集合中添加一个元素:
>>> mutableList.add...再写个代码示例:
>>> val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
>>> list.forEach { value -> if (value >...) println("value of index $index is $value, greater than 8") }
value of index 9 is 9, greater than...代码示例:
>>> val list = listOf(1,2,3,4)
>>> list.sumBy({it})
10
>>> list.sumBy({it*it})
30
类型错误反例:
>>> val