12
在Scala中使用索引进行有效的迭代
由于Scala没有for带有索引的旧Java样式循环, // does not work val xs = Array("first", "second", "third") for (i=0; i<xs.length; i++) { println("String #" + i + " is " + xs(i)) } 如何在不使用var的情况下有效地进行迭代? 你可以这样做 val xs = Array("first", "second", "third") val indexed = xs zipWithIndex for (x <- indexed) println("String #" + x._2 + " …