Kotlin Array Tutorials With Examples

Kotlin array is represented by Array class, that has get and set functions, size property and other member functions. Like other languages, Array in kotlin is a collection of similar data type i.e. int, string, float etc.

Kotlin Array is mutable in nature i.e. we can perform both read and write operation on elements of array.

Note – You can not change size of the array in kotlin.

Constructor of Array in Kotlin

Constructor of array is declared as shown below –

Array(size: Int, init: (Int) -> T) 

As you can see, constructor of array takes a fix size and an init function to return elements of array.

How to Create Array in Kotlin

There are multiple ways to create a kotlin array –
A. Using Library function
B. Using Array Constructor
C. Using Factory function

A. Create Kotlin Array Using Library Function

We can create array in kotlin using library function like arrayOf() , arrayOfNulls() .

Using arrayOf() function, we can create array as below –

var elements = arrayOf(1, 2, 3, 4, 5, 6)

Here, we have created a kotlin array of size 6 with elements 1, 2, 3, 4, 5 and 6. Note that all the elements are of same data type i.e. Int .

Using arrayOfNulls() function, we can create an array as below –

var elements = arrayOfNulls<Int>(6)

Here, we have created a kotlin array of size 6 and each element in the array is initialized with null value. You must provide data type of element of array while creating array. Here, it is Int.

B. Create Kotlin array using Array Constructor

We can create array in kotlin using Array constructor as below –

var elements = Array(6, { i -> (i * i).toString() })

As described earlier, parameter of Array constructor takes size of array and an init function that returns elements of array in kotlin.
Here, we are creating an array of size 6 that has element “0”, “1”, “4”, “9”, “16” and “25” . Note that all the elements are string not int.

C. Create Kotlin array using Factory function

Array of primitives data types (Byte, Short , Int etc. ) in kotlin have specialised class. For example, Byte has ByteArray, Int has IntArray, Short has ShortArray and so on. These classes has no inheritance relation with Array class. But each class has same set of methods and properties. Each such class has it’s own factory function. For example, byteArrayOf() for ByteArray, intArrayOf() for IntArray, floatArrayOf() for FloatArray and so on.

We can create array using factory function as below –

var byteArray = byteArrayOf(1, 2, 3, 4, 5, 6)
var intArray = intArrayOf(1, 2, 3, 4, 5, 6)
var floatArray = floatArrayOf(1.1f, 2.0f, 3.3f, 4.4f, 5.5f, 6.6f)
var shortArray = shortArrayOf(1, 2, 3, 4, 5, 6)
var doubleArray = doubleArrayOf(1.1, 2.2, 3.3, 4.4, 5.5, 6.6)

Here, we have shown how can you use factory function to create an array in kotlin. Each array has 6 elements in it.

Access elements of Array in Kotlin

There are 2 ways to access elements of array in kotlin and perform some operations on them.
A. Using indexing property.
B. Using get() functions.

A. Access elements of array in kotlin using indexing property

Let’s say we have an array (named arr) of size n. Each element in array has an index. Indexing in array starts from 0. For example, 1st position element has index 0, 2nd position element has index 1, 3rd position element has index 2 etc.

//Image to show index…

Thus, element at ith position in array is represented by arr[i-1].

Example –

fun main(args: Array<String>) {
   var elements = Array(6, { i -> (i * i).toString() })
   print(elements[3])  // Here i is 4. We are accessing elements at 4th position  
}

When you run the program, you will get output as below –
9

1. Guess the output of below program

fun main(args: Array<String>) {
   var elements = Array(6, { i -> (i * i).toString() })
   print(elements[6])
}

B. Access elements of array in kotlin using get() function

You can also access any element of array using get() function as below –

var elements = Array(10, { i -> (i * i).toString() })
println(elements.get(3))

Here, elements is an array of size 10. Using get() method, we are accessing element at 4th position in the array. This is similar to index property. You need to provide index i – 1 as parameter in get() method to access the element at ith position.

Here, we are accessing element at 4th position.

Example –

fun main(args: Array<String>) {
   var elements = Array(10, { i -> (i * i).toString() })
   println(elements.get(3))
}

When you run the program, you will get output –
9

Elements in the array are “0”, “1”, “4”, “9”, “16”, “25”, “36”, “49”, “64” and “81”. At 4th position, element is 9. So, answer is 9.

1. Guess the output of below program –

fun main(args: Array<String>) {
   var elements = Array(10, { i -> (i * i).toString() })
   println(elements.get(10))
}

Modify elements of array in kotlin using indexing property

You can modify the elements of array in below ways –
A. Using indexing property
B. Using set() function

A. Modify elements in kotlin array using indexing property

You can modify elements of array using indexing property as below –

array[i - 1] = newValue

Here, we are modifying the elements at ith position in the array. So, new value at ith position will be newValue

Example –

fun main(args: Array<String>) {
   var elements = arrayOf(1, 2, 3, 4, 5, 6)
   println("elements at 3rd position before change: ${elements[2]}")
   elements[2] = 10;
   println("elements at 3rd position after change: ${elements[2]}")
}

When you run above program, you will get output –
elements at 3rd position before change: 3
elements at 3rd position after change: 10

1. Guess the output of the program

fun main(args: Array<String>) {
   var elements = arrayOf(1, 2, 3, 4, 5, 6)
   println("elements at 3rd position before change: ${elements[2]}")
   elements[6] = 10;
   println("elements at 3rd position after change: ${elements[2]}")
}

B. Modify elements of array in kotlin array using set() function

We can modify elements of array using set() as below –

array.set(i - 1, newValue)

Here, we are modifying the element at ith position in the array with new value newValue.

Example –

fun main(args: Array<String>) {
   var elements = arrayOf(1, 2, 3, 4, 5, 6)
   println("elements at 3rd position before change: ${elements[2]}")
   elements.set(2, 10);
   println("elements at 3rd position after change: ${elements[2]}")
}

When you run the program, you will get output as shown below –
elements at 3rd position before change: 3
elements at 3rd position after change: 10

1. Guess the output of the program

fun main(args: Array<String>) {
   var elements = arrayOf(1, 2, 3, 4, 5, 6)
   println("elements at 3rd position before change: ${elements[2]}")
   elements.set(6, 10);
   println("elements at 3rd position after change: ${elements[2]}")
}

Traverse elements of array in kotlin using loop

You can traverse each element of array using loop in kotlin. So, now we will discuss on –
A. Traverse an array using for loop
B. Traverse an array using while loop
C. Traverse an array using do while loop

A. Traverse an array using for loop

Using for loop, you can traverse as below –

fun main(args: Array<String>) {
   val elements = arrayOf(1, 2, 3, 4, 5, 6)
   for (element in elements) {
       println(element)
   }
}

When you run the program, you will get output as below –
1
2
3
4
5
6

B. Traverse an array using while loop

Using while loop , you can traverse an array as below –

fun main(args: Array<String>) {
   val elements = arrayOf(1, 2, 3, 4, 5, 6)
   var i = 0;
   while (i < elements.size) {
       println(elements[i++])  // Access (i-1)th element
   }
}

When you run the program, you will get output as below –
1
2
3
4
5
6

C. Traverse an array using do while loop

Using do while loop, you can traverse an array as below –

fun main(args: Array<String>) {
   val elements = arrayOf(1, 2, 3, 4, 5, 6)
   var i = 0;
   do {
       println(elements[i++])
   } while (i < elements.size)
}

When you run the program, you will get output as below –
1
2
3
4
5
6

Exercises on Array in Kotlin

1. Guess the output of below program.

fun main(args: Array<String>) {
   val elements = Array<Int>(6) { 0 }
   for (element in elements) {
       println(element)
   }
}

2. Guess the output of below program.

fun main(args: Array<String>) {
   val elements = Array<Int>(6) { 0 }
   elements[4] = 10
   elements[5] = 6
   for (element in elements) {
       println(element)
   }
}

3. Guess the output of below program.

fun main(args: Array<String>) {
   val elements = Array<Int>(6) { 0 }
   elements[6] = 10
   for (element in elements) {
       println(element)
   }
}

4. Guess the output of below program.

fun main(args: Array<String>) {
   val elements = Array<Int>(3) { 0 }
   for (index in 0..elements.size-1) {
       println(elements[index])
   }
}

5. Guess the output of below program.

fun main(args: Array<String>) {
   val elements = Array<Int>(3) { 0 }
   for (index in 0..elements.size) {
       println(elements[index])
   }
}

That’s end of tutorial on Kotlin Array.

Leave a Reply