在下面的代码中,在for
理解内,我可以使用元组解引用来引用字符串和索引:
val strings = List("a", "b", "c")
for (stringWithIndex <- strings.zipWithIndex) {
// Do something with stringWithIndex._1 (string) and stringWithIndex._2 (index)
}
Scala 语法中是否有任何方法可以在理解标头中拆分为stringWithIndex
部分(字符串和索引),以便代码读者不必怀疑and的值?for
stringWithIndex._1
stringWithIndex._2
我尝试了以下方法,但无法编译:
for (case (string, index) <- strings.zipWithIndex) {
// Do something with string and index
}