Kotlin Training Program

DOWNLOAD APP

FEEDBACK

Arrays

Array data structure is used to store multiple items of same data type in a linear fashion.

Limitation : Once created, size of an array can not be changed. Hence, adding and removing elements is not supported, we can only update the elements.

Creating Array

We can use the arrayOf() function to create a new array and pass the array elements as arguments to the function. Syntax :

 arrayOf(/* Elements */)
 

Example :

 fun main() {
    val nums = arrayOf(12, 15, 20, 35) // Creates an array of 4 ints
}
 

We can create a new int array using IntArray constructor too. We have to pass a size in it and it’ll return an array of that size full of zeroes :

 IntArray(/* Elements */)
 

Example :

 fun main() {
    val nums = IntArray(3) // Creates an array of 3 ints (zeroes)
}
 

Similarly, we can create FloatArray, DoubleArray, LongArray, BooleanArray & CharArray. Numerical arrays are filled with zeroes, BooleanArray with false and CharArray with character of code 0.

Printing Array

To print all the elements of an array, we can use the predefined contentToString() function :

 /* array */.contentToString()
 

Example :

 fun main() {
    val nums = arrayOf(1, 2, 3)
    println("nums = ${nums.contentToString()}")
}
 

Alternatively, we can iterate (loop) through the array, visit every item and print it. We can use the for loop or the forEach function like this :

 fun main() {
    val nums = arrayOf(12, 15, 20, 35)

    // Using for loop 
    for (num in nums) {
        print("$num, ")
    }

    // OR Using forEach function
    nums.forEach {
        print("$it, ")
    }
}

/* Output :

12, 15, 20, 35, 
*/
 

While looping through an array, if we want index along with the element, we can use forEachIndexed function :

 /* array */.forEachIndexed { index, element ->
	// Code
}
 

Example :

 fun main() {
    val nums = arrayOf(12, 15, 20, 35)

    nums.forEachIndexed { index, i ->
        println("nums[$index] = $i")
    }
}

/* Output :

nums[0] = 12
nums[1] = 15
nums[2] = 20
nums[3] = 35
 */
 

Element Access

In Array, elements are arranged in linear fashion so each element has a fixed position (index). Indexing starts from 0 so first element is stored at index 0, second element at index 1 and so on. If an array is of size n then indices are in range 0…(n-1).

To access or modify an element, we need its index. We can access the element at a given index using indexing operator [] or .get() function :

 fun main() {
    val nums = arrayOf(12, 15, 20, 35)

    // Using indexing operator []
    println(nums[2]) // Prints 3rd element : 20

    // OR Using .get() function
    println(nums.get(0)) // Prints 1st element : 12
}
 

Check existence of an Element

To check whether an element is present in the Array, we can use the contains() function :

 /* array */.contains(/* Element */)
 

Example :

 fun main() {
    val nums = arrayOf(1, 2, 3)
    println("contains(1) = ${nums.contains(1)}")
    println("contains(4) = ${nums.contains(4)}")
}

/* Output :
contains(1) = true
contains(4) = false
*/
 

Modify element

To modify the element at a given index, we can use either the indexing operator & assignment operator or the set() function :

 array[/* index */] = /* new value */
// OR
array.set(/* index */, /* new value */)
 

Example :

 fun main() {
    val nums = arrayOf(12, 15, 20, 35)

    println("nums[0] = ${nums[0]}") // Prints 12 
    
    // Update 1st element to 10
    nums[0] = 10    
    // Update 3rd element to 25
    nums.set(2, 25)

    println("nums[0] = ${nums[0]}") // Prints 10
    println("nums[2] = ${nums[2]}") // Prints 25
}
 

Exceptions