Kotlin Arrays Example

In this tutorial, we will learn about Arrays in Kotlin with lots of examples. We will also see some of the important Kotlin Array operations or methods.

An array is a collection of a fixed number of values. The array items are called elements of the array. Each element can be referred to by an index. Arrays are zero-based.

Kotlin arrays are created with functions such as arrayOf() or intArrayOf(), or with classes such as IntArray or FloatArray.

Example 1: Create and Initialize an Array in Kotlin

The below Kotlin program demonstrates how we can create and initialize arrays in Kotlin:
import java.util.Arrays

fun main() {

    val nums = arrayOf(1, 2, 3, 4, 5)
    println(Arrays.toString(nums))

    val nums2 = (3..12).toList().toTypedArray()
    println(Arrays.toString(nums2))

    val nums3 = IntArray(5, { i -> i * 2 + 3})
    println(Arrays.toString(nums3))
}
Output:
[1, 2, 3, 4, 5]
[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
[3, 5, 7, 9, 11]

Example 2: Kotlin Array Basic Operations

In this Kotlin example, we will see how to retrieve and modify array elements, create a slice, and get an index of an element:
import java.util.Arrays

fun main() {

    val nums = arrayOf(1, 2, 3, 4, 5)
    println(nums.get(0))

    nums.set(0, 0)
    println(Arrays.toString(nums))

    val nums2 = nums.plus(1)
    println(Arrays.toString(nums2))

    val slice = nums.sliceArray(1..3)
    println(Arrays.toString(slice))

    println(nums.first())
    println(nums.last())
    println(nums.indexOf(5))
}
Output:
1
[0, 2, 3, 4, 5]
[0, 2, 3, 4, 5, 1]
[2, 3, 4]
0
5
4

Example 3: Kotlin Array Traversal or Looping

In this Kotlin example, we will discuss four different ways to loop over an Array in Kotlin.

fun main() {

    val nums = arrayOf(1, 2, 3, 4, 5, 6, 7)

    nums.forEach({ e -> print("$e ") })

    println()

    nums.forEachIndexed({i, e -> println("nums[$i] = $e")})

    for (e in nums) {
        print("$e ")
    }

    println()

    val it: Iterator<Int> = nums.iterator()

    while (it.hasNext()) {
        
        val e = it.next()
        print("$e ")
    }
}
Output:
1 2 3 4 5 6 7 
nums[0] = 1
nums[1] = 2
nums[2] = 3
nums[3] = 4
nums[4] = 5
nums[5] = 6
nums[6] = 7
1 2 3 4 5 6 7 
1 2 3 4 5 6 7 

Example 4: Kotlin Array count

The example counts the total number of values and the number of even values in the array.
fun main() {

    val nums = intArrayOf(2, 3, 4, 5, 6, 7)

    println("There are ${nums.count()} elements in the array")

    val nOfEvens = nums.count { it % 2 == 0 }

    println("There are $nOfEvens even values in the array")
}
Output:
There are 6 elements in the array
There are 3 even values in the array

Example 5: Sorting Arrays in Kotlin

The below Kotlin example demonstrates how to sort an array in ascending order with sortedArray() and descending order with sortedArrayDescending():
fun main() {

    val nums = arrayOf(7, 3, 3, 4, 5, 9, 1)

    val sortedNums = nums.sortedArray()
    println(Arrays.toString(sortedNums))

    val sortedNumsDesc = nums.sortedArrayDescending()
    println(Arrays.toString(sortedNumsDesc))
}
Output:
[1, 3, 3, 4, 5, 7, 9]
[9, 7, 5, 4, 3, 3, 1]

Example 6: Kotlin Filtering Arrays

In this Kotlin example, we will discuss how to filter arrays in Kotlin with an example.

The example creates an array of positive and negative integers. The filter() method is used to pick up only positive values.
fun main() {

    val nums = arrayOf(1, -2, 3, 4, -5, 7)

    nums.filter { e -> e > 0 }.forEach { e -> print("$e ") }
}
Output:
1 3 4 7 



Comments