0
爲什麼下面的代碼返回空列表而不是具有奇數位置值的列表?具有奇數位置上的值的返回列表
def f(arr:List[Int]) : List[Int] = {
def odd_concat(list_odd:List[Int], arr_index:Int) : List[Int] = {
if(arr_index == arr.size) {
list_odd
}
else if(arr_index % 2 == 0) {
odd_concat(list_odd, arr_index + 1)
}
else {
//println(arr(arr_index))
list_odd:+arr(arr_index)
odd_concat(list_odd, arr_index + 1)
}
}
odd_concat(List(), 0)
}
在我看來,多一點你的方法的功能和更清晰的:'arr.zipWithIndex.filter(T => T ._2%2!= 0).map(t => t._1)' – Brian
或者,'arr.sliding(2,2).flatMap(_。tail).toList' – jwvh