Kotlin: List vs Array — what's the catch?
\n\nIf you've been writing in Kotlin for at least a week, you've probably encountered the question: "Why can't I just use Array everywhere? Why do I need these List and MutableList?"
This question is one of the most popular on Stack Overflow. And for good reason. At first glance, both lists and arrays store elements, you can access them by index, and you can iterate over them. But dig deeper — and you'll find fundamental differences that affect performance, code safety, and even how you think when designing architecture.
\n\nIn this tutorial, we'll break down the difference between List and Array in Kotlin. You'll learn when to use an array and when to use a list, and why experienced developers almost always choose List.
1. Basics: what is Array in Kotlin?
\n\nArray in Kotlin is a direct projection of a Java array. It is a fixed sequence of elements of the same type. When you create an array, you immediately allocate memory for it. You cannot change the size of an array after creation.
// Creating an array\nval numbers: Array<Int> = arrayOf(1, 2, 3, 4, 5)\nprintln(numbers[0]) // 1\nnumbers[2] = 10 // you can change the value\nprintln(numbers[2]) // 10\n\nArrays in Kotlin are mutable by nature: you can always replace an element by index. But you cannot add a new element or delete an existing one. The size is fixed forever.
\n\nArray features:
\n- \n
- Fixed size — you cannot add or delete an element. \n
- Mutable content — you can overwrite elements. \n
- Performance — maximum, as it is direct memory access. \n
- Primitive arrays — there are special types:
IntArray,ByteArray,FloatArray, etc. They work without boxing, which gives a speed boost. \n
// Primitive array — without boxing\nval intArray: IntArray = intArrayOf(1, 2, 3, 4, 5)\n\n2. Basics: what is List in Kotlin?
\n\nList is an interface from the Kotlin standard library. Unlike an array, a list is an abstraction. You don't know exactly how it stores data internally (it could be ArrayList, LinkedList, or something else).
// Creating an immutable list\nval fruits: List<String> = listOf("apple", "banana", "cherry")\nprintln(fruits[0]) // apple\n// fruits[0] = "orange" // Compilation error!\n\nThe main "feature" of Kotlin is the separation into read-only (List) and mutable (MutableList).
List (read-only):
\n- \n
- You cannot change the content (neither add, delete, nor replace). \n
- You can only read. \n
- Safer for passing data between components. \n
MutableList:
\n- \n
- You can add, delete, and replace elements. \n
- Dynamic size. \n
- In practice,
ArrayListis most often used under the hood. \n
val mutableFruits: MutableList<String> = mutableListOf("apple", "banana")\nmutableFruits.add("orange") // OK\nmutableFruits.remove("banana") // OK\nmutableFruits[0] = "pear" // OK\n\n3. Key differences between List and Array
\n\nLet's summarize everything in a clear table (in code — as comments).
\n\n// 1. Mutability\nval array = arrayOf(1, 2, 3)\narray[0] = 99 // ✅ Works\n\nval list = listOf(1, 2, 3)\n// list[0] = 99 // ❌ Compilation error\n\nval mutableList = mutableListOf(1, 2, 3)\nmutableList[0] = 99 // ✅ Works\n\n// 2. Size\nval fixedArray = arrayOf(1, 2, 3)\n// fixedArray.add(4) // ❌ No such method exists\n\nva