ProgramGuru

Arrays and indexed access

Arrays and indexed access

A fixed row of marks can use an array, where indexed positions refer to elements. Kotlin distinguishes generic Array from specialized IntArray, which avoids boxing each integer on the JVM. Array size is fixed, although its elements can be replaced.

What you’ll learn

  • Trace aliases and an explicit array copy.
  • Handle an out-of-range lookup safely.

Arrays and indexed access in the example

  1. Shared reference

    A fixed row of marks can use an array, where indexed positions refer to elements.

    Shared reference
  2. Separate copy

    Zero is the first index, and size is one past the last valid position.

    Separate copy
  3. Observe different elements

    Alias and scores refer to the same array, so the update is visible as 75.

    Observe different elements
Alias and scores refer to the same array, so the update is visible as 75. Copy retains 70 because copyOf created separate element storage. The absent indexed lookup returns null.

Understand arrays and indexed access

Zero is the first index, and size is one past the last valid position. An invalid subscript throws instead of returning null. getOrNull provides a nullable lookup. Assigning an array to another variable shares its reference; use copyOf for a separate array value.

Connect this to read-only and mutable lists and classes, objects and shared references.

Follow arrays and indexed access

Save the complete source as Main.kt and use the Kotlin/JVM environment. No separate input file is needed.

Kotlin/JVM · complete program

fun main() {
  val scores = intArrayOf(70, 80)
  val alias = scores
  val copy = scores.copyOf()
  alias[0] = 75
  println(scores[0])
  println(copy[0])
  println(scores.getOrNull(5))
}

Expected output

75
70
null

Follow the execution

Alias and scores refer to the same array, so the update is visible as 75. Copy retains 70 because copyOf created separate element storage. The absent indexed lookup returns null.

Try this in the Kotlin compiler by pasting the complete program.

Practice: Arrays and indexed access

Your task: Does val prevent alias[0] from changing?

Kotlin/JVM · complete program

fun main() {
  val scores = intArrayOf(70, 80)
  val alias = scores
  val copy = scores.copyOf()
  alias[0] = 75
  println(scores[0])
  println(copy[0])
  println(scores.getOrNull(5))
}

Worked answer

No. Val prevents reassigning the array reference, not replacement of its mutable elements.

Arrays and indexed access: the important boundary

An array copy is shallow if its elements are object references. Copying their slots does not clone the objects. For growable ordered collections, use an appropriate list instead of pretending append can change an array’s fixed size.

Check your understanding

Does val prevent alias[0] from changing?
Choose one answer. You can try again.

Answer and explanation

No.

No. Val prevents reassigning the array reference, not replacement of its mutable elements.

Arrays and indexed access: what to keep in mind

No. Val prevents reassigning the array reference, not replacement of its mutable elements. An array copy is shallow if its elements are object references.

Further reading: official Kotlin documentation for this topic.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Read-only and mutable lists →