> ## 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.

# Kotlin Array

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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
// 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.

```kotlin theme={"dark"}
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** (`*`).

```kotlin theme={"dark"}
fun printAll(vararg messages: String) {
    // Implementation
}

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

// The '*' operator unpacks the array elements into varargs
printAll(*args) 
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
