【发布时间】:2020-05-27 21:39:38
【问题描述】:
我有一个向量列表。每个向量以Vector(Double,Double) 的形式表示一个范围。我想创建一个给定输入数字的函数,找到它包含在哪个向量中并返回该向量的索引。我不知道是否有更简单的方法可以做到这一点,我是 Scala 新手,但我的代码如下:
val vectors = #List of vectors ( List[scala.collection.immutable.IndexedSeq[Double]] )
def in_range(start: Double, end: Double, x : Double): Boolean = {(x>= start && x<end)}
def find_index(x:Double): Int = {
for(i <- 0 to n){
if( in_range(vectors(i)(0),vectors(i)(1),x)){
return i
}
}
我收到以下错误:
<console>:28: error: type mismatch;
found : Unit
required: Int
for(i <- 0 to 10){
^
【问题讨论】:
标签: scala