Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

An array in Kotlin is a mutable, fixed-size collection of elements of a uniform type, represented by the Array<T> class. On the JVM, Kotlin arrays are backed by standard Java arrays. A critical distinction in Kotlin’s type system is that arrays are invariant. This means Array<String> is not considered a subtype of Array<Any>, preventing ArrayStoreException runtime errors that can occur in Java.

Array Instantiation

Kotlin provides several standard library functions and constructors to instantiate arrays: 1. Using arrayOf and arrayOfNulls The most direct method is using factory functions. arrayOf creates an array with specified elements, while arrayOfNulls creates an array of a given size filled with null values.
// Type inferred as Array<Int>
val numbers = arrayOf(1, 2, 3, 4)

// Explicit type declaration
val strings = arrayOf<String>("Kotlin", "Java", "C++")

// Creates an Array<String?> of size 5, initialized with nulls
val nulls = arrayOfNulls<String>(5)
2. Using the Array Constructor The Array class constructor takes two parameters: the size of the array and an initialization lambda function (Int) -> T that returns the initial value for each index.
// Creates an array of size 5: ["0", "1", "4", "9", "16"]
val squares = Array(5) { index -> (index * index).toString() }

Primitive Arrays

Using Array<Int> or Array<Double> results in the boxing of primitive types on the JVM (e.g., Integer[]), which incurs memory and performance overhead. To represent unboxed primitive arrays (e.g., int[]), Kotlin provides specialized classes: ByteArray, ShortArray, IntArray, LongArray, FloatArray, DoubleArray, BooleanArray, and CharArray. These classes have no inheritance relationship with the Array<T> class but expose identical methods and properties.
// Unboxed int[] array
val unboxedInts = intArrayOf(1, 2, 3)

// Unboxed double[] array of size 3, initialized to [0.0, 0.0, 0.0]
val unboxedDoubles = DoubleArray(3)

// Unboxed char[] array initialized via lambda: ['A', 'B', 'C']
val unboxedChars = CharArray(3) { index -> ('A' + index) }

Access and Modification

Array elements are accessed and modified using the indexed access operator [], which the Kotlin compiler translates into calls to the get() and set() member functions.
val data = arrayOf(10, 20, 30)

// Read access (translates to data.get(0))
val first = data[0] 

// Write access (translates to data.set(1, 50))
data[1] = 50 

Interoperability and the Spread Operator

When calling Java methods that accept variable-length arguments (vararg), or Kotlin functions with vararg parameters, an existing array cannot be passed directly as a single argument. It must be unpacked using the spread operator (*).
fun printAll(vararg messages: String) {
    // Implementation
}

val args = arrayOf("Error", "Warning", "Info")

// The '*' operator unpacks the array elements into varargs
printAll(*args) 
Master Kotlin with Deep Grasping Methodology!Learn More