Tutorials

How Can You Use Arrays in Kotlin?

How to Use Arrays in Kotlin

Arrays are fundamental data structures in practically all computer languages. An array is a data structure that stores several pieces of the same data type, such as an integer or string, under a single variable name. An array is a collection of data of the same kind that has been grouped. Simultaneously, the amount of these data elements is known ahead of time, and each component has its location in the array.

Arrays are used in programming to organize data such that the related set of values can be readily sorted or found.

Here are some fundamental properties of arrays:

  • They are stored in adjacent memory cells.
  • They can be accessed through programs and their indexes (array[1], array[0] , and so on)
  • They are volatile.
  • Their size is fixed.

Kotlin has “traditional” arrays, which can only contain one data type and cannot be expanded, unlike a list.

Arrays are often declared with value, although this does not exclude you from changing the values of array items. A collection can only be allocated to an immutable variable once. A Var variable can be assigned a new array.

Creating an Array

Array initialization is possible using both functions and class constructors. The example below uses the built-in arrayOf () function, which is passed a set of values.

fun main() {

    val a: Array<Int> = arrayOf(9, 3, 4)

    println(a.size)

    a[0] = 10

    for (i in a) {

        println(i)

    }

}

Using the array constructor

Since Array is a class in Kotlin, we can also use the Array constructor to create an array.

The constructor accepts two settings:

  1. The size of the array
  2. A function that takes the index of a given element and returns the initial value of that element

Initializing three array elements with zero when using the class constructor:

fun main() {

    val a: Array<Int> = Array(3) {0}

    a[0] = 5

    for (i in a) {

        println(i)

    }

}

The constructor of the Array class takes as its second argument a lambda expression enclosed in curly brackets, and it generates values. Such arguments in Kotlin are usually placed in parentheses.

Using get() and set() methods

When querying and modifying the values of items, the standard array syntax with square brackets is utilised. They are overridden in the class by the get() and set() functions. They can also be addressed by name. The get() function takes a single parameter—the element’s index—and returns the value of the element at that point.

Arrays are represented in Kotlin by more than just the Array class. There are particular classes (and accompanying functions) for constructing arrays with primitive type members, such as BooleanArray, ByteArray, ShortArray, IntArray, LongArray, CharArray, FloatArray, and DoubleArray. They make it a little easier to make arrays.

For numeric types, there are also variants of unsigned arrays-UByteArray, UShortArray, UIntArray, ULongArray.

You can use the in and !in Boolean operators to check for specific values in an array.

fun main() {

    val a: BooleanArray = booleanArrayOf(true, false, false)

    val b: IntArray = intArrayOf(5, 3, 1, 2)

 

    println(true in a)

    println(0 in b)

}

Read more about Multi-platform Projects in Kotlin here


.

You may also like...