Problem Statement
How do you create an immutable list in Kotlin?
Explanation
The listOf function creates a read-only List where you cannot add or remove elements, like val numbers equals listOf open paren 1 comma 2 comma 3 close paren. For mutable lists that can be modified, use mutableListOf which returns a MutableList.
Kotlin's list creation functions are more concise than Java's verbose ArrayList instantiation and initialization. You can also use emptyList for empty lists or listOfNotNull to filter out null values automatically.
Read-only doesn't mean truly immutable as the underlying implementation might be mutable, but the interface prevents modifications through that reference, providing compile-time safety against accidental changes.
Practice Sets
This question appears in the following practice sets:
