🐦 Kotlin Beginner

How do you create a list in Kotlin?

Answer

Kotlin distinguishes between read-only and mutable collections. Use listOf(1, 2, 3) to create an immutable list — you cannot add, remove, or update elements. Use mutableListOf(1, 2, 3) to create a mutable list that you can modify. Both are backed by Java's ArrayList. For an empty list, use emptyList() or listOf(). Kotlin also has arrayListOf() for an explicit ArrayList. Always prefer the read-only listOf() by default and only use mutable when necessary — this makes code safer and easier to reason about.