扁平的MutableList元素每隔若干元素一组装入新MutableList,Kotlin
Kotlin
fun main(args: Array<String>) {
val array = arrayOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j")
val STEP = 3 //3个元素一组
var k = 0
val lists = mutableListOf<MutableList<String>>()
for (i in array.indices step STEP) {
val temp = mutableListOf<String>()
for (j in 0 until STEP) {
k = i + j
if (k >= array.size) {
break
}
temp.add(array[k])
}
lists.add(temp)
}
lists.forEachIndexed { index, s ->
println(s)
}
}
[a, b, c]
[d, e, f]
[g, h, i]
[j]