Kotlin · Course navigation

Kotlin Lists

Process an ordered collection.

On this page

The concept

listOf creates a read-only list. A for loop visits each item in order and prints it.

Code example

Kotlin
fun main() {
    val scores = listOf(82, 91, 76)
    for (score in scores) {
        println(score)
    }
}
Output
82
91
76

Follow the code

Open the walkthrough to inspect the example one step at a time.

Show guided walkthrough
Guided walkthroughReady
1fun main() {
2 val scores = listOf(82, 91, 76)
3 for (score in scores) {
4 println(score)
5 }
6}

Follow the example

Start to inspect the source, values, and output. These prepared steps explain the published sample; grouped loop steps describe multiple iterations.

View the complete dry-run table
Prepared trace of the published program. Some rows group repeated iterations.
Step / lineWhat happensState after stepOutput so far
Create a list containing three scores.scores = [82, 91, 76]
—
Visit each score in sequence.scores = [82, 91, 76]
—
Print each visited value.scores = [82, 91, 76]
82
91
76

Behind the scenes

How Kotlin processes this code

Kotlin commonly compiles to JVM bytecode, which the JVM executes.