Efficient iteration with index in Scala

前端 未结 12 534
后悔当初
后悔当初 2020-12-12 11:57

Since Scala does not have old Java style for loops with index,

// does not work
val xs = Array(\"first\", \"second\", \"third\")
for (i=0; i<         


        
12条回答
  •  囚心锁ツ
    2020-12-12 12:03

    One more way:

    scala> val xs = Array("first", "second", "third")
    xs: Array[java.lang.String] = Array(first, second, third)
    
    scala> for (i <- xs.indices)
         |   println(i + ": " + xs(i))
    0: first
    1: second
    2: third
    

提交回复
热议问题