Kotlin Arrays

We’re gonna learn about arrays in Kotlin. Arrays are nothing but continuous memory locations referred with a common name.

Arrays are a collection of elements. The individual elements in an array are accessed using the index. The index always starts at 0. That means, the first element in an array is stored in zeroth position and the second element is stored in the first position and so on. If the length of the array is fixed as n, the index of the last element would be n-1. The size of the array is fixed. That is an array can never grow in size. Look at the diagram:

kotlin arrays
kotlin arrays

Why we use arrays?

We’ve seen some basics of arrays. Now let’s see why we need to use arrays?
Imagine a situation where we want to store the names of 50 students in a class. We can of course use 50 variables for storing it. But it’s cumbersome and inefficient. In these kinds of situations, we use arrays. You’ll see how efficient they are when we use them in our programs.

Now let’s see how to declare and use arrays in our Kotlin program.

fun main() {
    val valArray = Array<String>(10) { "" }
    valArray[0] = "Audrey"
    valArray[1] = "Ken"
    valArray[2] = "Zoe"
    println(valArray[0])
    println(valArray[1])
    println(valArray[2])
}

In this program, we’re using an array of length 10 to hold some names. See how we assign values to the individual elements. And how we’re accessing the values by using the index.

See the line in which the array is declared. val valArray = Array(10) { “” }. Notice that we used the keyword val. We want to assign things to individual elements, but we’re not changing the reference to any other object. Arrays are objects. We’re not changing the reference. Hence immutable type is fine here.

In the above program we’re using array to hold values but we’re not using it’s power.

Now let’s see how to use arrays normally.

fun main() {
    val valArray = Array<String>(10) { "" }
    println("Enter the names")
    for (index in valArray.indices)
        valArray[index]= readLine()!!
    println("The entered names are")
    for (vals in valArray)
        println(vals)
}

See the above program. First, we declare an array of type String and length 10. We initialize the values with an empty string. We then use a for loop to receive all 10 names from the user. See how are we using the for loop to get the indices of the individual elements in the array. After getting all the 10 names, we’re using another for loop to print the values in the array.

arrayOf() function

We can declare an array in an even simpler fashion if we know the initial values of elements. We use the function arrayOf() for that.
See this:

fun main() {
    val valArray = arrayOf(22,4,36,21,12,84,33)
    for (vals in valArray)
        println(vals)
}

Getting the size of the array

We can get the size of the array using the size property of the array object.
See this example

fun main() {
    val valArray = arrayOf(22,4,36,21,12,84,33)
    for (vals in valArray)
        println(vals)
    println("Size of the array : ${valArray.size}")
}

We can also mix various type of values in the array using arrayOf() function.

fun main() {
    val valArray = arrayOf(22, 12, 84, 33, "Watson", "Ashley")
    for (vals in valArray)
        println(vals)
    println("Size of the array : ${valArray.size}")
}

Creating an array from a range

We can create an array from a range of numbers. In other words, we can convert a range of numbers into an array.

fun main() {
    val valArray = (1..10).toList().toTypedArray()
    for (vals in valArray)
        println(vals)
    println("Size of the array : ${valArray.size}")
}

We use two functions here. First we defined a range. We convert it into a list by calling the function toList() on it. Then we convert it into an array using the function toTypedArray().

Hope you got a basic idea of Kotlin arrays from this post. Did I miss any important thing. Feel free to let me know by adding a comment below. Thank you
See you in the next post.
Happy Coding đŸ™‚

« Back to the index of Kotlin tutorial

Please follow and like us:

Leave a Reply