How to Use Arrays in Kotlin?

How to Use Arrays in Kotlin

An array is one of the basic data structures in almost all programming languages. The idea of an array is to store multiple elements of the same data type, such as an integer or string, under a single variable name. An array can be represented as data of the same type grouped together. At the same time, the number of these data elements is known in advance, and each element has its own place in the array.

Arrays are used to organize data in programming in order that the associated set of values can be easily 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 “classic” arrays, when only one type of data can be in one array, and elements cannot be added to the array, as in a list.

Arrays are usually declared using val, but this doesn’t mean that you can’t change the value of array elements. An immutable variable can only be assigned an array once. You can assign a new array to a Var variable.

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 elements and changing their values, the usual array syntax with square brackets is used. In the class, they are overridden by the get() and set() methods. They can also be called directly by name. The get() method accepts a single parameter-the index of the element and returns the value of the element at this index.

In Kotlin, arrays are represented not only by the Array class. There are special classes (and their corresponding functions) for creating arrays containing elements of primitive types – BooleanArray, ByteArray, ShortArray, IntArray, LongArray, CharArray, FloatArray, DoubleArray. They make it somewhat easier to create 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) // true

    println(0 in b) // false

}

Read more about Multi-platform Projects in Kotlin here

Recommended Articles

Share
Tweet
Pin
Share
Share