What is an array in Kotlin?

An array is used to store similar data type values in a single variable. For example, you may create fifty integer or string type variables and store a value in each.

Alternatively, you may create a single array of int or string type and store fifty values in it.

A few main points about Kotlin arrays:

  • The array class represents an array in Kotlin.
  • The array class has get and set functions.
  • The array class also has the size property.
  • Just like in Java, the items in the Kotlin array are called elements.
  • Elements in the array can be accessed by index number.
  • The index starts at zero.
  • Kotlin arrays also have a few other useful member functions.

How to create an array in Kotlin?

You may create arrays in different ways:

By using the library function

You may use arrayOf() library function for creating an array. For example:

var NumArray = arrayOf(1,2,3,4,5)

Similarly, you may use a specific type of library function for creating an array. These include:

  • charArrayOf()
  • booleanArrayOf()
  • byteArrayOf()
  • intArrayOf()
  • longArrayOf()
  • shortArrayOf()

You may also create an array as follows:

var arrNum = IntArray(size)

Or

An int array can also be created like this:

var arrNum = Array(size, { 0 } )

The following section gives more detail about how to create, initialize and access array elements in Kotlin with complete code. The examples also include using a few useful array functions.

An example of using arrayOf() function

In the first example of Kotlin arrays, I am using arrayOf() library function for creating an array that contains five integer type elements.

This is followed by using a for loop for accessing those array elements:

The output:

5

10

15

20

25

The example of a string array by arrayOf function

The example below initializes an array of strings by using the arraOf() function. Notice, how the string data type is specified in the code below.

Again, I iterated through that array by using a for loop for displaying its items:

The result:

New York

London

Dubai

Accessing array elements by index example

Just like in Java, the index of array elements in Kotlin starts at zero. The example below shows accessing the array elements by index number:

The output:

The country name at 0 index: USA

The example of array get() function

Now let us look at a few useful array functions and property in Kotlin. The first is the get() function that is used to return the array element at the specified index.

It takes an argument which is the index number (int type). The example below shows using the get() function for accessing an element:

The output:

Kotlin array

The example of set() function

The set() function is used to set the array element value at the specified index position. It takes two arguments; first is the index and the other is the value to be set.

operator fun set(index: Int, value: T)

The example below shows how to use this function. I have created an array of four elements of string type. By using the set() function, I will modify the third element value:

The result:

Kotlin array set

The count function example

The count function returns the number of elements in this array. See the example below for its usage:

The output:

Total elements in the array: 19

A distinct function example

If you require to get the list of distinct elements in an array then use the distinct() function. An array is specified and distinct() returns the list of unique items.

The example below contains duplicate elements. I used distinct function and see how it returns the list of unique items:

The output of the above code:

Unique elements in ArrNum: [5, 4, 3, 2, 1]

Unique elements in ArrChar: [z, d, y, t]

drop and dropLast functions

The drop function returns a list of elements in the array except for the first n elements. The n is an integer value that you specify for removing the first elements.

Similarly, the dropLast returns a list of array elements except for the n from the last. See the example of both functions below:

The output of the above code:

After drop with 3 value: [20, 25, 30, 35, 40, 45, 50]

After dropLast with 3 value: [5, 10, 15, 20, 25, 30, 35]

first and last functions example

The first() function of the array returns the first element while the last() returns the last element of the array. Have a look at the example of both:

Output:

First element in array: 10

Last element of array: 50

An example of the size property

The size is a property of an array. It returns the total number of elements in the array. See the example code for its usage:

Result:

The size of array: 5

Sorting the array by sort() function

The sort() function is used to sort a range in the array in place. For example:

The output:

Kotlin array sort

You may learn more about the array functions in the Kotlin official website here.