小编Vis*_*han的帖子

Scala:索引Seq,而不是for循环中的List

以下代码显示类型不匹配错误:

def f(arr:List[Int]): List[Int] = 
  for(num <- 0 to arr.length-1; if num % 2 == 1) yield arr(num)
Run Code Online (Sandbox Code Playgroud)

据说它找到了IndexedSeq而不是List。以下作品:

def f(arr:List[Int]): List[Int] = 
  for(num <- (0 to arr.length-1).toList; if num % 2 == 1) yield arr(num)
Run Code Online (Sandbox Code Playgroud)

i <- a to b之前在for循环中使用过,但之前没有看到此错误。有人可以解释为什么i <- a to b不能在此处使用该格式吗?

scala

2
推荐指数
1
解决办法
1850
查看次数

标签 统计

scala ×1