Sha*_*kti 0 scala scala-collections
我有以下两个列表,我不确定在运行时list2是空还是满,但list1将始终为非空,如何确保以下for循环值列表至少打印
val list1 = List(1,2,3) //> list1 : List[Int] = List(1, 2, 3)
val list2 = List() //> list2 : List[Nothing] = List()
for( counti <- list1 ; countj <- list2 ) yield println (counti + " - " + countj)
//> res7: List[Unit] = List()
Run Code Online (Sandbox Code Playgroud)
我期待着类似的东西
1 - BLANK
2 - BLANK
3 - BLANK
Run Code Online (Sandbox Code Playgroud)
但上面的循环给我空白结果列表()
for (
counti <- list1;
countj <- if(list2.nonEmpty) list2 else List("BLANK")
) {
println(counti + " - " + countj)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
954 次 |
| 最近记录: |