英文:
How to Get the True Count in Boolean Array in Kotlin
问题
我已阅读了 Kotlin 文档并尝试了 BooleanArray.count(predicate: (Boolean) -> Boolean)
,但它仍然返回的是 array
的大小,而不是 true
的数量:
val checks = arrayOf(true, false, true, true, true)
val trueCount = checks.count { true }
我的代码有什么问题?
英文:
I've read the kotlin documentation and tried BooleanArray.count(predicate: (Boolean) -> Boolean)
, but it still returns the size of the array
, not the number of true
:
val checks = arrayOf(true, false, true, true, true)
val trueCount = checks.count { true }
What's wrong with my code?
答案1
得分: 15
你在这里传递了一个始终会返回 true 的条件:
val trueCount = checks.count { true }
这将确实返回数组的大小。我预期你的代码应该是:
val trueCount = checks.count { it }
换句话说,对于“使用被称为 it
的项目,表达式 it
返回 true”的项目计数。
另一种可能更易读的替代方案是显式检查相等性:
val trueCount = checks.count { it == true }
就个人而言,我倾向于避免与布尔字面值进行比较,但我可以理解在这种情况下至少可以有理地更清晰。
英文:
You're passing in a predicate that would always return true here:
val trueCount = checks.count { true }
And that would indeed return the size of the array. I'd expect your code to be:
val trueCount = checks.count { it }
In other words, the count of items for which "with the item called it
, the expression it
returns true".
An alternative you might find easier to read that explicitly checks for equality:
val trueCount = checks.count { it == true }
Personally I tend to avoid comparisons against Boolean literals, but I can see how it's at least arguably clearer in this case.
答案2
得分: 8
使用 count { it }
替代 count { true }
count
函数的参数并不是要在数组中查找的值;它是一个谓词(predicate):一个接受一个参数并返回 true
或 false
的函数。这个谓词对数组中的每个项调用一次,由谓词返回的值(true
或 false
)决定是否对该项进行计数。因此,结果是数组中谓词评估为 true
的项的数量。
在这个上下文中,写 { true }
相当于 { value -> true }
,它是一个恒定的函数。无论 value
是什么,它始终返回 true
。因此,“计算使 { true }
返回 true
的项的数量” 正好等同于“计算项的数量”。
相反,你想要计算值为 true
的项的数量。你应该使用的谓词是 { value -> value }
,它是一个接受单个参数并返回该参数值的函数。由于当你不给参数命名时,单个 lambda 参数隐式命名为 it
,因此 { value -> value }
的简写形式就是 { it }
。
英文:
Use count { it }
instead of count { true }
The argument to count
isn't a value to look for in the array; it's a predicate: a function that takes one argument and returns true
or false
. The predicate is called once for each item in the array, and the value (true
or false
) returned by that predicate determines whether the item is counted or not. So the result is the number of items in the array for which the predicate evaluated to true
.
In this context, writing { true }
is shorthand for { value -> true }
, and is a constant function. It will always return true
, regardless of what the value
is. So "count the number of items for which { true }
returns true
" is exactly the same as "count the number of items".
What you want instead is to count the number of items where the value is true
. The predicate you should use is { value -> value }
, which is a function that takes a single argument and returns the value of that argument. Since a single lambda parameter is implicitly named it
when you don't give it a name, the shorthand for { value -> value }
is just { it }
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论