gpt4 book ai didi

kotlin - 如何在 kotlin 中使用函数式样式获取二维数组的列

转载 作者:行者123 更新时间:2023-12-02 12:19:16 26 4
gpt4 key购买 nike

例如,我有一个如此简单的任务:计算二维数组中所有全为零的原始数据和列数。所以对于

0 0 0
0 0 0
1 0 1

答案是 2 个原始数据和 1 个列。我可以像 raws 那样做:var cntRaws = a.count { it.all { el -> el == 0 } },但是如何以同样的方式解决列问题?

最佳答案

val x = Array<IntArray>(3) { IntArray(3) { 0 } }

x[2][0] = 1
x[2][2] = 1
val raws = x.count { it.sum() == 0 }
val columns = (x.indices)
.map { columnIndex -> x.map { it[columnIndex] } }
.count { it.sum() == 0 }

println("total raws:$raws")
println("total col:$columns")

关于kotlin - 如何在 kotlin 中使用函数式样式获取二维数组的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62474460/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com