AnyCompiler

Arrays and Lists

intArrayOf(...) creates a fixed-size array of primitive ints. for (n in numbers) iterates over any array or collection directly, with no index variable needed.

MutableList

mutableListOf(...) creates Kotlin's primary growable collection — it manages its own resizing as you add elements. The plain listOf(...) (no 'mutable') creates a read-only list instead, with no add method at all — another place Kotlin nudges you toward immutability by default.

fun main() {
    val numbers = intArrayOf(4, 8, 15, 16, 23)
    var sum = 0
    for (n in numbers) {
        sum += n
    }
    println(sum)

    val list = mutableListOf(1, 2, 3)
    list.add(4)
    println(list.size)
}
Output
66
4

Next: strings — string templates, Kotlin's built-in interpolation syntax.

Practice exercise

Given val numbers = intArrayOf(4, 8, 15, 16), sum all its elements with a for loop and print the total.