Scala Vector.toArray
Supply an indexed array of names to a printer from an ordered collection.
A simple example
Vector.toArray
- Start with List containing A and B
- Do this Convert to an array
- Result Array containing A and B
Syntax
roster.toArray
Run these examples in Scala
Save one example at a time as Lesson.scala. Use Scala 2.13 on JDK 21.
scalac Lesson.scala
scala LessonSend the badge roster to an older printer
A school stores its badge roster in a list, but the older printer driver accepts an array of names. The coordinator converts the roster before sending it to the printer in registration order.
object Lesson {
def main(args: Array[String]): Unit = {
val roster = Vector("Mira", "Sam")
val printJob: Array[String] = roster.toArray
println(printJob.mkString(", "))
}
}
Mira, Sam
The print job contains Mira followed by Sam.
Load a lobby display
The gallery sends an ordered set of rooms to a display that accepts an array. Transfer the ordered entries to an array for the display. Which sequence will its positions contain?
Send the last route update
Only one shuttle stop is left in the current schedule. Transfer the ordered entries to an array for the display. Which sequence will its positions contain?
What to watch for
The array can be edited independently of the Vector, although mutable element objects may still be shared.