Question
In Java, you can initialize an array like this:
int numbers[] = new int[] {10, 20, 30, 40, 50};
How do you initialize an array with values in Kotlin?
Short Answer
By the end of this page, you will understand how to create arrays in Kotlin using predefined values, when to use arrayOf(...), when to use primitive array types like intArrayOf(...), and how Kotlin array syntax differs from Java.
Concept
Kotlin supports arrays, but its syntax is different from Java.
In Java, array creation often uses a type followed by square brackets and a new expression. Kotlin does not use new, and array creation is usually done through functions such as arrayOf(...).
For example:
val numbers = arrayOf(10, 20, 30, 40, 50)
This creates an Array<Int>.
However, Kotlin also has primitive array types for better performance:
IntArrayDoubleArrayBooleanArrayCharArrayLongArrayFloatArrayShortArrayByteArray
So if you specifically want an array of primitive Int values, Kotlin commonly uses:
val numbers = intArrayOf(10, , , , )
Mental Model
Think of a Kotlin array as a row of numbered boxes.
- Each box has a position:
0,1,2, and so on. - You can fill the boxes with values when you create the array.
arrayOf(...)is like saying: "Create a row of boxes and put these items in them."intArrayOf(...)is like saying: "Create a row of integer boxes specifically forIntvalues."
So instead of Java's new int[] {...}, Kotlin says: "Use this array-building function and provide the values."
Syntax and Examples
Basic array initialization
val numbers = arrayOf(10, 20, 30, 40, 50)
This creates an Array<Int>.
Primitive integer array
val numbers = intArrayOf(10, 20, 30, 40, 50)
This creates an IntArray.
String array
val names = arrayOf("Alice", "Bob", "Charlie")
This creates an Array<String>.
Accessing values
val numbers = intArrayOf(10, 20, 30)
println(numbers[0]) // 10
println(numbers[1])
Step by Step Execution
Consider this example:
val numbers = intArrayOf(10, 20, 30)
println(numbers[0])
println(numbers[2])
Step by step:
intArrayOf(10, 20, 30)creates an array containing three integers.- The array is stored in the variable
numbers. numbers[0]reads the first element, which is10.println(numbers[0])prints10.numbers[2]reads the third element, which is30.println(numbers[2])prints30.
Output:
10
30
Here is another traceable example:
val values = Array(4) { index -> index + 1 }
println(values.joinToString())
Real World Use Cases
Arrays in Kotlin are useful when you need fixed-size indexed data.
Common examples
- Storing numeric data such as scores, coordinates, or sensor readings
- Working with Java APIs that expect arrays
- Processing command-line arguments or fixed sets of values
- Image or audio data where indexed access matters
- Performance-sensitive code using primitive arrays like
IntArray
Example: fixed monthly sales values
val sales = intArrayOf(120, 150, 90, 200)
println(sales[3]) // 200
Example: passing arrays to functions
fun printNames(names: Array<String>) {
for (name in names) {
println(name)
}
}
val team = arrayOf("Ana", "Ben", "Chris")
printNames(team)
Example: generated values
val evenNumbers = IntArray() { it * }
println(evenNumbers.joinToString())
Real Codebase Usage
In real Kotlin codebases, arrays are used less often than lists, but they are still important.
Common patterns
Interoperating with Java
Many Java libraries use arrays in method parameters or return types.
val args = arrayOf("--debug", "--port=8080")
Using primitive arrays for performance
If a program handles many numeric values, developers often prefer primitive arrays.
val temperatures = DoubleArray(7)
Initializing from known constants
Configuration-like values may be stored in arrays when the size is fixed.
val supportedCodes = arrayOf("en", "es", "fr")
Transforming data for APIs
Developers may build arrays from collections before calling functions that require arrays.
val namesList = listOf("Alice", "Bob")
val namesArray = namesList.toTypedArray()
Guarding against invalid access
Because arrays are index-based, developers often validate indices before use.
Common Mistakes
1. Expecting Java syntax to work in Kotlin
Broken code:
val numbers = new int[] {10, 20, 30}
Why it fails:
- Kotlin does not use
new. - Kotlin does not use Java's array literal syntax.
Correct version:
val numbers = intArrayOf(10, 20, 30)
2. Confusing Array<Int> with IntArray
val a = arrayOf(1, 2, 3) // Array<Int>
val b = intArrayOf(1, 2, 3) // IntArray
These are not the same type.
Avoid this mistake by checking what a function expects.
3. Using the wrong constructor style
Broken idea:
Comparisons
Arrays in Kotlin compared
| Concept | Syntax | Size | Typical use |
|---|---|---|---|
Array<T> | arrayOf(1, 2, 3) | Fixed | General object values |
IntArray | intArrayOf(1, 2, 3) | Fixed | Primitive integers, better performance |
List<T> | listOf(1, 2, 3) | Read-only interface | Most everyday Kotlin code |
MutableList<T> | mutableListOf(1, 2, 3) | Resizable |
Cheat Sheet
Quick syntax
val a = arrayOf(1, 2, 3) // Array<Int>
val b = intArrayOf(1, 2, 3) // IntArray
val c = arrayOf("a", "b") // Array<String>
val d = Array(3) { it * 10 } // Array<Int>: [0, 10, 20]
val e = IntArray(3) { it + 1 } // IntArray: [1, 2, 3]
Rules to remember
- Kotlin does not use
newfor arrays. arrayOf(...)creates a generic object array.intArrayOf(...)creates a primitive integer array.- Arrays have fixed size.
- Indexing starts at
0. - Use
array[index]to read or update values.
Useful helpers
println(arr.size)
println(arr.indices)
println(arr.joinToString())
FAQ
How do you initialize an array in Kotlin with values?
Use arrayOf(...) for general arrays or intArrayOf(...) for primitive integers.
val numbers = intArrayOf(10, 20, 30)
What is the Kotlin equivalent of new int[] { ... } in Java?
The closest Kotlin equivalent is:
val numbers = intArrayOf(10, 20, 30)
Should I use arrayOf or intArrayOf in Kotlin?
Use arrayOf(...) for generic object arrays. Use intArrayOf(...) when you need an IntArray with primitive integers.
Are Kotlin arrays fixed size?
Yes. Once created, an array's size cannot change.
What is the difference between Array<Int> and List<Int> in Kotlin?
Mini Project
Description
Build a small Kotlin program that stores a week's daily temperatures in an array and prints useful information. This project demonstrates how to initialize arrays with values, access elements by index, and iterate through array contents.
Goal
Create a Kotlin program that initializes an array with temperature values and prints the first, last, and all temperatures.
Requirements
- Create an integer array containing 7 temperature values.
- Print the first temperature using index access.
- Print the last temperature using its index.
- Loop through the array and print every temperature.
- Print the full array in a readable format.
Keep learning
Related questions
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.
Can You Extend a Data Class in Kotlin? Inheritance, Limits, and Better Alternatives
Learn why Kotlin data classes cannot be extended, what causes the component function clash, and which alternatives to use instead.
Difference Between List and Array in Kotlin
Learn the difference between List and Array in Kotlin, including mutability, size, APIs, performance, and when to use each one.