marcosbeirigo
marcosbeirigo

Reputation: 11328

Scala: Best way to iterate over collection and populate Array

scala noob here, i have a collection (Seq) of xml nodes, and i would like to populate an Array based on each node:

val nodes = data.child \\"package"
var packages = new Array[Package](nodes.length)
var index = 0
for(val entry <- nodes) {
   packages(index) = new Package(entry)
   index = index+1
}

Although it works, does not look much "scala-ish" to me, and i'm sure there's a better way to do it..
Any ideas?

Upvotes: 5

Views: 1667

Answers (1)

Vasil Remeniuk
Vasil Remeniuk

Reputation: 20617

(data.child \\ "package") map(new Package(_)) toArray

Upvotes: 12

Related Questions