Scala数组的间隔数的索引(Scala the index of a number in which interval of an array)

编程入门 行业动态 更新时间:2024-10-04 13:26:51
Scala数组的间隔数的索引(Scala the index of a number in which interval of an array)

假设我有一个(0, 0.1, 0.2, ..., 1)的数组,我有一个数字0.15 ,我希望得到它的索引。 由于0.15 > 0.1和0.15 < 0.2 ,因此它在区间2 。 有没有最先进的方法来做到这一点? 目前我使用带有var变量的for循环并遍历每个间隔,但我不认为这是最好的方法。

Suppose I have an array of (0, 0.1, 0.2, ..., 1) and I have a number 0.15, I'd hope to get the index of that. Since 0.15 > 0.1 and 0.15 < 0.2, it is in the interval 2. Is there a state-of-art way to do that? Currently I use a for loop with a var variable and go through each interval but I do not think it's the best way.

最满意答案

假设数组已订购,您可以尝试:

arr.indexWhere(_ >= 0.15) - 1

如果没有这样的实例,你会得到 - 2。

assuming the array is ordered you can try:

arr.indexWhere(_ >= 0.15) - 1

if there is no such instance you get - 2.

更多推荐

本文发布于:2023-08-07 11:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464099.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   间隔   索引   Scala   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!