Вы можете написать функцию расширения, которая преобразует MutableList<Boolean>
в MutableList<Int>
и затем использует forEach
в списке списков для преобразования каждого элемента:
// extension function for an Int-representation of a Boolean-list
fun MutableList<Boolean>.toIntList(): MutableList<Int> {
var result: MutableList<Int> = mutableListOf()
this.forEach { it -> if (it) { result.add(1) } else { result.add(0) } }
return result
}
fun main(args: Array<String>) {
// example Boolean-matrix
var matrix: MutableList<MutableList<Boolean>> = mutableListOf(
mutableListOf(true, true, true),
mutableListOf(false, false, false),
mutableListOf(false, true, false),
mutableListOf(true, false, true)
)
// provide the structure for the result
val newMatrix: MutableList<MutableList<Int>> = mutableListOf()
// for each Boolean-list in the source list add the result of toIntList() to the result
matrix.forEach { it -> newMatrix.add(it.toIntList()) }
// print the source list
println(matrix)
// print the resulting Int list
println(newMatrix)
}
Вывод:
[[true, true, true], [false, false, false], [false, true, false], [true, false, true]]
[[1, 1, 1], [0, 0, 0], [0, 1, 0], [1, 0, 1]]
Могут быть разные и даже лучшие способы конвертации, но этого достаточно.