Kotlin Array Basic Operations

The following example presents some basic operations with Kotlin arrays.

Kotlin Array Basic Operations Examples

In the example, we retrieve and modify array elements, create a slice, and get an index of an element.
package net.javaguides.kotlin.examples

import java.util.Arrays

fun main(args: Array < String > ) {
    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





Comments