In Kotlin, an Array stores a collection of elements of the same types. Array stores elements in contiguous memory locations, like each element is stored in a sequential memory address though each element place next to another. Arrays in Kotlin are Statically typed so the type of element stored in an array is determined at once when the array is created and its nonchangeable. Array are fixed size, meaning once we create an array with elements then its size cannot be changed. We can modify its elements but adding and removing elements is restricted. So basically we can use an array whenever we know how many elements we are going to store in a fixed number.
Arrays in Kotlin: A Comprehensive Guide With Examples
Creating an Array in Kotlin:
1. Using arrayOf() method.
1 2 3 |
val numbers = arrayOf(1, 2, 3, 4, 5) println(numbers.contentToString()) // Output: [1, 2, 3, 4, 5] |
2. Using intArrayOf() method.
1 2 3 4 5 |
// Creating an Int array val intArray = intArrayOf(10, 20, 30, 40) // Accessing and printing elements println("Original Int Array: ${intArray.joinToString()}") |
- Updating integer array elements.
12345// Updating an elementintArray[1] = 50 // Changing the second element (index 1) to 50// Printing the updated arrayprintln("Updated Int Array: ${intArray.joinToString()}") - Output
123Original Int Array: 10, 20, 30, 40Updated Int Array: 10, 50, 30, 40
3. To create a double-type array we will use doubleArrayOf() function.
1 2 3 4 5 |
// Creating a Double array val doubleArray = doubleArrayOf(1.1, 2.2, 3.3) // Accessing and printing elements println("Original Double Array: ${doubleArray.joinToString()}") |
- Modifying or Updating double array elements.
12345// Updating an elementdoubleArray[0] = 5.5 // Changing the first element (index 0) to 5.5// Printing the updated arrayprintln("Updated Double Array: ${doubleArray.joinToString()}") - Output:
123Original Double Array: 1.1, 2.2, 3.3Updated Double Array: 5.5, 2.2, 3.3
4. To create a String array we will be using arrayOf() method.
1 2 3 4 5 |
// Creating a String array val stringArray = arrayOf("Kotlin", "Java", "Python") // Accessing and printing elements println("Original String Array: ${stringArray.joinToString()}") |
- Updating String array elements.
12345// Updating an elementstringArray[2] = "C++" // Changing the third element (index 2) to "C++"// Printing the updated arrayprintln("Updated String Array: ${stringArray.joinToString()}") - Output:
123Original String Array: Kotlin, Java, PythonUpdated String Array: Kotlin, Java, C++
Creating an empty array with default value:
1 2 3 4 5 6 7 8 9 10 |
val emptyArray = Array(5) { 0 } // Creates an array with five 0's println(emptyArray.contentToString()) // Output: [0, 0, 0, 0, 0] val emptyArray2 = Array(5) { "hello world" } // Creates an array with five hello world println(emptyArray2.contentToString()) // Output: [hello world, hello world, hello world, hello world, hello world] |
Array Iterating(Printing array elements):
There are 3 ways to print or get array elements in Kotlin.
- Using Index Position – To print single element only.
123456789// Creating a String arrayval stringArray = arrayOf("Kotlin", "Java", "Python")// Printing a single element (for example, the second element)println("The second element is: ${stringArray[1]}")//OutputThe second element is: Java - Using for loop – To print complete array.
123456789101112// Creating a String arrayval stringArray = arrayOf("Kotlin", "Java", "Python")// Using a for loop to print each elementfor (element in stringArray) {println(element)}// OutputKotlinJavaPython - Using for each loop – to print the complete array.
12345678910111213// Creating a String arrayval stringArray = arrayOf("Kotlin", "Java", "Python")// Using forEach loop to print each elementstringArray.forEach { element ->println(element)}// OutputKotlinJavaPython
There are a ton of array inbuilt methods available for Kotlin. You can read them from HERE on Kotlin’s official page. In our next article, we will be covering all of them. Happy coding.