使用while
构造编写以下代码的替代方法是什么?
val list = List(1,2,3)
for (v <- list) println(v)
答案 0 :(得分:5)
val list = List(1,2,3)
val iter = list.iterator
while (iter.hasNext) println(iter.next())
答案 1 :(得分:3)
可能的强制性遍历:
var current = list
while(!current.isEmpty) {
println(current.head)
current = current.tail
}
答案 2 :(得分:0)
val list = List(1,2,3)
var i = 0
while (i < list.length) {
println(list(i))
i += 1
}