
Arrays are used to store multiple objects in a single variable. To create an array in Kotlin, we have to use the arrayOf() method. Write the objects separated by commas and pass them to the method. The method returns an Array instance.
Example:
val usersList = arrayOf("Steven", "Michelle", "Emma")
Here usersList is an array and Steven, Michelle, and Emma are its elements.
We can also specify the element’s data type explicitly.
val usersList = arrayOf<String>("Steven", "Michelle", "Emma")
Access Array Elements:
We can access array elements in 2 ways – using the index and get() method.
Using the index:
The index of an array starts from 0. Index 0 represents the first element, index 1 represents the second element, and so on…
In the above example:

fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
println(usersList[0]) // prints Steven
println(usersList[1]) // prints Michelle
println(usersList[2]) // prints Emma
}
Using the get() method:
The get() method takes the index of the element as an argument and returns the corresponding element.
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
println(usersList.get(0)) // prints Steven
println(usersList.get(1)) // prints Michelle
println(usersList.get(2)) // prints Emma
}
Update Array Elements:
There are two ways to update an array element – using the index and set() method.
Using the Index:
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
usersList[1] = "Kate" // changing 2nd element
println(usersList[1]) // prints Kate
}
Using the set() method:
The set() method takes two parameters – the index of the element and the new value.
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
usersList.set(index = 1, value = "Kate") // changing 2nd element
println(usersList[1]) // prints Kate
}
Array Size or Length:
To find the number of elements in an array, use the size property.
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
println(usersList.size) // prints 3
}
Check If an Element Exists:
We can use the in operator to check if an element exists in an array. It returns true if the element is found.
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
if ("Steven" in usersList) {
println("Element Exist")
} else {
println("Element Doesn't exist")
}
}
Output:
Element Exist
Note: The in operator comparison is case sensitive.
Get All Array Elements:
There are multiple ways to get all the elements of an array.
for loop with in operator:
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
for (user in usersList) {
println(user)
}
}
Output:
Steven
Michelle
Emma
Using the indices Property:
The indices property returns the range of valid indices of the array.
fun main() {
val usersList = arrayOf("Steven", "Michelle", "Emma")
for (i in usersList.indices) {
println("usersList[$i] = " + usersList[i])
}
}
Output:
usersList[0] = Steven
usersList[1] = Michelle
usersList[2] = Emma
Note: The arrayOf() method accepts multiple data types.
fun main() {
val list1 = arrayOf("Steven", 100, 'k', 22.9, Animal())
for (element in list1) {
println(element)
}
}
data class Animal(var name: String = "Cow")
Output:
Steven
100
k
22.9
Animal(name=Cow)
Instead of the arrayOf() method, we can also use the Array class. Its constructor takes two parameters – size and init function. The size is the array size (number of elements) and init is a lambda function which initializes the elements.
fun main() {
val numbers = Array(size = 4) { i -> i * 3 }
for (number in numbers) {
println(number)
}
}
Output:
0
3
6
9
Kotlin also provides other methods to create arrays with primitive data types such as intArrayOf().
fun main() {
val numbers = intArrayOf(10, 20, 30)
for (number in numbers) {
println(number)
}
}
Output:
10
20
30
The intArrayOf() method returns IntArray instance. Similar to the Array, we can also create an integer array using the IntArray class.
fun main() {
val numbers = IntArray(size = 4) { i -> i * 2 }
for (number in numbers) {
println(number)
}
}
Output:
0
2
4
6
If we don’t mention the init function, all the elements are initialized with 0.
fun main() {
val numbers = IntArray(size = 4)
for (number in numbers) {
println(number)
}
}
Output:
0
0
0
0
Other methods are:
- doubleArrayOf()
- floatArrayOf()
- longArrayOf()
- intArrayOf()
- charArrayOf()
- shortArrayOf()
- byteArrayOf()
- booleanArrayOf()
How to Add New Elements to an Kotlin Array?
There is no way to add new elements to an existing array. The size of an array is fixed. If you want to resize an array, consider using Kotlin Collections instead.
That said, the array provides a method called plus(). It accepts an element and returns a new array containing all the elements in the given array and the passed element.
fun main() {
val array1 = arrayOf(1, 2, 3)
val array2 = array1.plus(100)
for (element in array2) {
println(element)
}
}
Output:
1
2
3
100
This is all about Array methods and classes in Kotlin. I hope you learned something new. If you have any doubts, feel free to comment below.