Kotlin Arrays
An array stores many values of the same type in one place. Instead of creating five separate variables for five test scores, you create one array that holds all five. Arrays keep related data together and let you process it with loops.
A Row of Lockers
Picture a hallway with a row of numbered lockers. Each locker holds one item, and each locker has a fixed number. The row never grows or shrinks. An array follows the same plan. The numbers are called indexes, and the counting starts at 0.
Index: 0 1 2 3 4
+------+------+------+------+------+
Value: | 72 | 85 | 90 | 64 | 78 |
+------+------+------+------+------+
Size = 5, last index = 4
Creating an Array
The arrayOf function builds an array from values you provide.
val scores = arrayOf(72, 85, 90, 64, 78)
val names = arrayOf("Asha", "Ravi", "Mina")Kotlin offers specialized arrays for plain numbers. These types skip extra wrapping and run faster:
val ints = intArrayOf(1, 2, 3)
val doubles = doubleArrayOf(1.5, 2.5)
val flags = booleanArrayOf(true, false)Creating with a Rule
The Array constructor takes a size and a rule. The rule runs once for every index.
val squares = Array(5) { index -> index * index }
println(squares.contentToString()) // [0, 1, 4, 9, 16]An empty array of fixed size comes from IntArray(5). Every slot starts at 0.
Reading and Changing Values
Square brackets pick a locker by its index.
val scores = arrayOf(72, 85, 90, 64, 78)
println(scores[2]) // 90
scores[3] = 70 // change one value
println(scores[3]) // 70
println(scores.size) // 5The size is fixed. An array never adds a sixth slot. Use a list when the amount of data changes.
Printing an Array
Calling println on an array shows a memory label instead of the values. The contentToString() function prints the real content.
println(scores) // [Ljava.lang.Integer;@1b6d3586
println(scores.contentToString()) // [72, 85, 90, 70, 78]Looping Through an Array
for (score in scores) {
println(score)
}
for (i in scores.indices) {
println("Locker $i holds ${scores[i]}")
}
for ((i, score) in scores.withIndex()) {
println("$i: $score")
}The first loop reads values only. The second loop reads positions through indices. The third loop delivers both pieces together.
Index Out of Bounds
Asking for a locker that does not exist crashes the program. An array of size 5 has indexes 0 to 4. Index 5 triggers an ArrayIndexOutOfBoundsException.
println(scores[5]) // crashThe helper getOrNull(5) returns null instead of crashing.
Useful Array Functions
val numbers = intArrayOf(5, 3, 8, 1)
println(numbers.sum()) // 17
println(numbers.average()) // 4.25
numbers.sort()
println(numbers.contentToString()) // [1, 3, 5, 8]
println(3 in numbers) // true
println(numbers.copyOf(2).contentToString()) // [1, 3]
println(numbers.reversedArray().contentToString()) // [8, 5, 3, 1]Two-Dimensional Arrays
An array can hold other arrays. The result forms a grid, like seats in a cinema with rows and columns.
col 0 col 1 col 2
row 0 [ 1 2 3 ]
row 1 [ 4 5 6 ]
row 2 [ 7 8 9 ]
val grid = Array(3) { row ->
IntArray(3) { col -> row * 3 + col + 1 }
}
println(grid[1][2]) // 6
for (row in grid) {
println(row.contentToString())
}The first bracket picks the row. The second bracket picks the column.
Array or List
| Feature | Array | List |
|---|---|---|
| Size | Fixed | Fixed or growing (mutable list) |
| Speed for numbers | Very fast with IntArray | Slightly slower |
| Printing | Needs contentToString() | Prints values directly |
| Common use | Fixed data, performance work | Everyday data handling |
