Kotlin Lists

Process an ordered collection.

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

Explanation

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

Explore the dry run
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