Arrays in Kotlin: Don't Be Afraid, It's Simple
\n\nIf you are switching from Java to Kotlin or are just starting to learn this modern language, the question "how to initialize an array in Kotlin with values" arises almost immediately. On Stack Overflow, this topic has gathered thousands of views — which means the topic is truly important.
\n\nThe good news: Kotlin has made working with arrays much more convenient than in Java. The bad news: there are several ways, and beginners often get confused. Let's once and for all break down all the options for initializing a Kotlin array — from simple to complex, with real code examples.
\n\n1. The Simplest Way: arrayOf()
\n\nThe arrayOf() function is your bread and butter. It creates an array from the passed values, automatically determining the type.
// Array of integers\nval numbers = arrayOf(1, 2, 3, 4, 5)\n\n// Array of strings\nval fruits = arrayOf("apple", "banana", "orange")\n\n// Array of mixed type (Any)\nval mixed = arrayOf(42, "Kotlin", true)\n\nprintln(numbers.contentToString()) // [1, 2, 3, 4, 5]\nprintln(fruits.contentToString()) // [apple, banana, orange]\n\n\nImportant: arrayOf() creates an Array<T> — an object array. If you need primitive types (int, char, boolean) for performance, read on.
2. Primitive Arrays: intArrayOf(), doubleArrayOf() and Others
\n\nKotlin offers specialized functions for each primitive type. They save memory and work faster:
\n\n// Integers\nval intArr = intArrayOf(10, 20, 30, 40, 50)\n\n// Floating point numbers\nval doubleArr = doubleArrayOf(1.5, 2.7, 3.14)\n\n// Boolean values\nval boolArr = booleanArrayOf(true, false, true)\n\n// Characters\nval charArr = charArrayOf('K', 'o', 't', 'l', 'i', 'n')\n\n// Bytes\nval byteArr = byteArrayOf(0x48, 0x65, 0x6C, 0x6C, 0x6F) // "Hello"\n\nprintln(intArr.joinToString()) // 10, 20, 30, 40, 50\n\n\nFull list: intArrayOf, doubleArrayOf, floatArrayOf, longArrayOf, shortArrayOf, byteArrayOf, booleanArrayOf, charArrayOf.
When to use? If you are working with large data or writing performance-critical code — use primitive arrays. For regular tasks, arrayOf() is perfectly sufficient.
3. Array Constructor: Array(size) with a Lambda
\n\nThe most flexible way to initialize an array in Kotlin is using the constructor with a lambda expression. You specify the size and the filling rule:
\n\n// Squares of numbers from 0 to 4\nval squares = Array(5) { i -> i * i }\nprintln(squares.contentToString()) // [0, 1, 4, 9, 16]\n\n// Array of 10 random numbers\nimport kotlin.random.Random\nval randoms = Array(10) { Random.nextInt(100) }\nprintln(randoms.contentToString()) // [42, 17, 83, 5, 91, ...]\n\n// Array of strings with a template\nval labels = Array(3) { index -> "Element #${index + 1}" }\nprintln(labels.contentToString()) // [Element #1, Element #2, Element #3]\n\n\nThe lambda receives the element index (from 0 to size-1) and must return a value. This is a powerful tool — you can fill an array according to any rule, including calling functions.
\n\n4. arrayOfNulls() — Array with Empty Cells
\n\nSometimes you need to create an array and fill it later. For this, there is arrayOfNulls():
// Array of 5 null elements\nval nullArray = arrayOfNulls<String>(5)\nprintln(nullArray.contentToString()) // [null, null, null, null, null]\n\n// Fill later\nnullArray[0] = "First"\nnullArray[2] = "Third"\nprintln(nullArray.contentToString()) // [First, null, Third, null, null]\n\n\nThe type must be specified explicitly — the compile