Efficient iteration with index in Scala

前端 未结 12 548
后悔当初
后悔当初 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:26

    A simple and efficient way, inspired from the implementation of transform in SeqLike.scala

        var i = 0
        xs foreach { el =>
          println("String #" + i + " is " + xs(i))
          i += 1
        }
    

提交回复
热议问题