Start Coding

Topics

Kotlin Arrays

Arrays in Kotlin are fixed-size collections that store elements of the same type. They provide a convenient way to work with ordered sets of data, offering efficient access and manipulation of elements.

Creating Arrays

Kotlin offers several ways to create arrays:

1. Using arrayOf() function

val numbers = arrayOf(1, 2, 3, 4, 5)
val fruits = arrayOf("apple", "banana", "orange")

2. Using Array constructor

val squares = Array(5) { i -> i * i }
// Creates an array: [0, 1, 4, 9, 16]

3. Using specialized array types

Kotlin provides specialized classes for primitive types to avoid boxing overhead:

val intArray = intArrayOf(1, 2, 3, 4, 5)
val doubleArray = doubleArrayOf(1.0, 2.0, 3.0)

Accessing and Modifying Array Elements

Array elements can be accessed and modified using index notation:

val fruits = arrayOf("apple", "banana", "orange")
println(fruits[1]) // Output: banana

fruits[1] = "grape"
println(fruits[1]) // Output: grape

Array Properties and Functions

Kotlin arrays come with useful properties and functions:

  • size: Returns the number of elements in the array
  • indices: Returns the valid indices range for the array
  • isEmpty(): Checks if the array is empty
  • contains(element): Checks if the array contains a specific element

Iterating Over Arrays

Kotlin provides several ways to iterate over arrays:

1. Using for loop

val numbers = arrayOf(1, 2, 3, 4, 5)
for (number in numbers) {
    println(number)
}

2. Using indices

val fruits = arrayOf("apple", "banana", "orange")
for (i in fruits.indices) {
    println("$i: ${fruits[i]}")
}

3. Using withIndex()

val colors = arrayOf("red", "green", "blue")
for ((index, value) in colors.withIndex()) {
    println("$index: $value")
}

Array Operations

Kotlin arrays support various operations:

1. Sorting

val numbers = arrayOf(3, 1, 4, 1, 5, 9)
numbers.sort()
println(numbers.contentToString()) // Output: [1, 1, 3, 4, 5, 9]

2. Filtering

val numbers = arrayOf(1, 2, 3, 4, 5)
val evenNumbers = numbers.filter { it % 2 == 0 }
println(evenNumbers) // Output: [2, 4]

3. Mapping

val numbers = arrayOf(1, 2, 3, 4, 5)
val squared = numbers.map { it * it }
println(squared) // Output: [1, 4, 9, 16, 25]

Multi-dimensional Arrays

Kotlin supports multi-dimensional arrays, which are essentially arrays of arrays:

val matrix = Array(3) { Array(3) { 0 } }
matrix[0][0] = 1
matrix[1][1] = 5
matrix[2][2] = 9

for (row in matrix) {
    println(row.contentToString())
}

Best Practices

  • Use specialized array types (e.g., IntArray, DoubleArray) for better performance when working with primitive types.
  • Consider using Kotlin Lists for more flexible, resizable collections.
  • Utilize Kotlin Collection Operations for efficient array manipulation.
  • Be cautious with array bounds to avoid ArrayIndexOutOfBoundsException.

Arrays in Kotlin provide a powerful tool for managing ordered collections of data. By understanding their creation, manipulation, and common operations, you can effectively utilize arrays in your Kotlin programs.