【发布时间】:2019-08-28 02:31:23
【问题描述】:
结果列表的元素应该在参数的元素之间交替。假设两个参数的长度相同。
使用递归
我的代码如下
val finalString = new ListBuffer[Int]
val buff2= new ListBuffer[Int]
def alternate(xs:List[Int], ys:List[Int]):List[Int] = {
while (xs.nonEmpty) {
finalString += xs.head + ys.head
alternate(xs.tail,ys.tail)
}
return finalString.toList
}
预期结果:
alternate (List (1, 3, 5), List (2, 4, 6)) = List (1, 2, 3, 4, 6)
至于输出,我没有得到任何输出。程序仍在运行,无法执行。
有 Scala 专家吗?
【问题讨论】:
-
可能是交替的 ( List (1 , 3, 5) , List (2 , 4, 6)) = List (1 , 2, 3, 4, 5, 6 )?