Scala for循环具有多个变量

nuc*_*ear 2 java for-loop scala

如何将此循环(在Java中)转换为Scala?

for(int i = 0, j = 0; i < 10; i++, j++) {
  //Other code
}
Run Code Online (Sandbox Code Playgroud)

我的最终目标是同时遍历两个列表.我想在迭代中的每个索引处同时获取这两个元素.

for(a <- list1, b <- list2) // doesn't work

for(a <- list1; b <- list2) // Gives me the cross product
Run Code Online (Sandbox Code Playgroud)

kaw*_*wty 8

使用.zip()使元组的列表,并遍历它.

val a = Seq(1,2,3)
val b = Seq(4,5,6)
for ((i, j) <- a.zip(b)) {
  println(s"$i $j")
}
// It prints out:
// 1 4
// 2 5
// 3 6
Run Code Online (Sandbox Code Playgroud)