kotlin

Create an Array in Kotlin

Arrays in Kotlin are used to store one or multiple values and access them using their index that starts from 0. You can create implicit and explicit type Arrays in Kotlin. We use arrayOf() function of Kotlin to create an array.

fun main() {
    // array that contains any type of value - implicit
	val arr = arrayOf(10, 20, 30, 40, 50, "Jack")
    
    println(arr[1]) // -> 20
    println(arr[5]) // -> "Jack"
    
    // array that contains integer type values only - explicit
    val nums = arrayOf<Int>(3, 2, 9, 4)
    println(nums[2]) // -> 9
}

Output

20
Jack
9

Arrays are used widely in any programming language to contain and retrieve multiple data items of various data types. We can also create Arrays in Kotlin programming language to store multiple items in it and access the items using the index. The Arrays are iterable so we can loop through an Array and access its data items.

There are two types of declaration of Arrays in Kotlin.

Create an implicit type Array

The implicit type Array can contain any type of data like integers and Strings in the same array. You can add multiple items to the implicit type array without thinking of the type of element that you are adding. An example of creating an implicit type array is as below.

val arr = arrayOf(5, 9, "Devsheet", 10, "Kotlin", 50)

The above array contains multiple items of String and Integer types and you can access the elements of the above array using their indexes.

Create an explicit type Array

An explicit type array can contain only the same type of elements. For example, if you have created an Int values Array then it will store integer type values only, and the String values array will store string type values only. An example of a String type values array is as below.

val names = arrayOf<String>("Rick", "James", "Sumit", "Troy")

The above Kotlin Array can store String type values only. if you try to add integer or other type value it will throw an error - The integer literal does not conform to the expected type String.

An example of an explicit type array with integer values only

val nums = arrayOf<Int>(10, 30, 90, 100, 80)

Create an Array using built-in functions of Kotlin

We can also create Arrays in Kotlin using the build-in function of Kotlin. For example, if you want to create an integer values array you can use function - intArrayOf().

val nums = intArrayOf(10, 30, 90, 100, 80)

Other methods that can be used to create an Array in Kotlin are:

byteArrayOf()

shortArrayOf()

longArrayOf()

charArrayOf()

Get Length of an Array in Kotlin

To get the length of a Kotlin Array you can use Array.size. For example, if you have an array - nums and want to get the length of this array, you can use the below code.

Code Example

val nums = intArrayOf(10, 30, 90, 100, 80)
    
print(nums.size)

Output

5

Loop through an Array in Kotlin

The Arrays in Kotlin are iterable and you can loop through an Array and access its items in each iteration.

We will be using for loop here to loop through an Array.

Code Example

val fruits = arrayOf("Apple", "Banana", "Orange", "Grapes")
    
for (fruit in fruits) {
    println(fruit)
}

Output

Apple
Banana
Orange
Grapes

Check if an item exists in an Array

To check if an item exists inside an Array, we can use the 'in' operator. It will return true if the item exists in an array and return false if the item does not exist in the array.

val fruits = arrayOf("Apple", "Banana", "Orange", "Grapes")

if ("Orange" in fruits) {
    println("Item exists in the array")
} else {
    println("Item does not exist in the array")
}

Output

Item exists in the array

Access the items of an Array

We can access the items of an array using the item index. For example, if there is an array that contains 10 items in it and you want to get the second item from the array then you can use array[1] to get the item value. 

The index starts from 0 means if you want to get the first item you will use array[0].

val students = arrayOf<String>("John", "Pablo", "Martin", "Suresh", "Amit")
    
// Access third item from students array
print(students[2])

Output

Martin

Access items using Array.get()

We can also use Array.get() to get the items from an array using item index.

Syntax

Array.get(index)

Code example

val names = arrayOf<String>("James", "Walter", "Roy", "Salve")

println(names.get(1))
Was this helpful?