Library contents

Kotlin List.toTypedArray

Supply an indexed array of names to a printer from an ordered collection.

A simple example

List.toTypedArray

  1. Start with List containing A and B
  2. Do this Convert to an array
  3. Result Array containing A and B
The representation changes while the order and values stay the same.

Syntax

Kotlin syntax
roster.toTypedArray()
Run these examples in Kotlin

Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.

kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jar

Send 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.

From the programval roster = listOf("Mira", "Sam")
ResultMira, Sam
Lesson.kt
fun main() {
    val roster = listOf("Mira", "Sam")
    val printJob: Array<String> = roster.toTypedArray()
    println(printJob.joinToString(", "))
}
Output
Mira, Sam

The print job contains Mira followed by Sam.

Advertisements

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?

GivenClay · Glass · Ink
Worked resultClay · Glass · Ink

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?

GivenQuay
Worked resultQuay

What to watch for

This copies references into a new array. Use toIntArray for primitive IntArray output.

Documentation for List.toTypedArray