Skip to content
Lists and Arrays
step 1/5

Reading — step 1 of 5

Learn

~1 min readCollections

Kotlin distinguishes read-only and mutable collection types:

val immutable: List<Int> = listOf(1, 2, 3)
val mutable: MutableList<Int> = mutableListOf(1, 2, 3)
mutable.add(4)
// immutable.add(4)  // ✗ no add method on List

println(immutable.size)       // 3
println(immutable[0])         // 1
println(immutable.first())    // 1
println(immutable.last())     // 3

Default to List (read-only) and only use MutableList when you actually need to modify in place. listOf() makes a read-only list backed by an immutable array — the type system prevents mutation.

For primitive arrays without boxing: IntArray, DoubleArray, etc. — useful for performance-critical code.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…