Scala: Best way to iterate over collection and populate Array
Asked Answered
P

1

5

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?

Phosphoresce answered 2/12, 2010 at 16:21 Comment(0)
O
12
(data.child \\ "package") map(new Package(_)) toArray
Ophthalmology answered 2/12, 2010 at 17:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.