在的indexOf数组科特林 [英] indexOf in Kotlin Arrays
本文介绍了在的indexOf数组科特林的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何得到一个值的指数从科特林阵列?
How do I get the index of a value from a Kotlin array?
我的最好的解决方案,现在正在使用:
My best solution right now is using:
val max = nums.max()
val maxIdx = nums.indices.find({ (i) -> nums[i] == max }) ?: -1
有没有更好的办法?
is there a better way?
推荐答案
如果你想获得最大的元素的索引,您可以使用maxBy功能:
If you want to get the index of the maximum element you can use 'maxBy' function:
val maxIdx = nums.indices.maxBy { nums[it] } ?: -1
有更有效,因为它会遍历阵列只有一次。
It is more efficient since it will traverse the array only once.
这篇关于在的indexOf数组科特林的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文