我对 Kotlin 还很陌生。我想打印字符串中字符的计数。为此,我使用 Kotlin 的groupingBy()
功能及应用eachCount()
to it.
My code:
val str = "0100101101010"
val countMap : Map<Char, Int> = str.groupingBy { it }.eachCount()
println(countMap["1"])
但我在控制台中收到此错误:Type inference failed. The value of the type parameter K should be mentioned in input types (argument types, receiver type or expected type). Try to specify it explicitly.
有人可以向我解释我在这里做错了什么吗?
"1"
is a string字面意思,但你的地图有人物作为钥匙。您应该使用特点文字'1'
:
println(countMap['1'])
出现令人困惑的错误消息的原因是因为它试图调用this get超载 https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get.html,这是通用的。它尝试推断泛型类型参数,但失败了。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)