Arrays

An array is a fixed-size, indexed container that holds a sequence of values of the same type, accessed by position starting at zero with the [ ] operator.

Learn Arrays in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

You'll create arrays, read and update elements, iterate by value and by index, use the primitive IntArray , and build a simple 2D grid.

What You'll Learn in This Lesson

1️⃣ Creating & Updating Arrays

Use arrayOf(...) for any type and intArrayOf(...) for efficient integer arrays. Read an element with arr[i] and update it the same way. Note that a val array can still have its elements changed — val only locks the variable, not the contents.

2️⃣ Iterating: By Value and By Index

Loop the values directly with for (x in arr) , or loop the positions with for (i in arr.indices) when you need the index. The indices property is a ready-made range from 0 to size - 1 .

The primitive IntArray can be built from a size and an initializer lambda, and it ships with handy numeric helpers:

3️⃣ 2D Arrays

A two-dimensional array is simply an array whose elements are themselves arrays — perfect for grids, boards, and matrices. Address a cell with two indices: grid[row][col] .

Your turn. Fill in the ___ blanks, then run and compare.

Summarize a set of test scores with size , max() , and average() .

📋 Quick Reference — Arrays

Practice quiz

What is the main difference between an Array and a List in Kotlin?

  • An Array is always immutable
  • A List cannot be indexed
  • An Array has a fixed size; a MutableList can grow and shrink
  • They are identical

Answer: An Array has a fixed size; a MutableList can grow and shrink. Array size is fixed at creation; use a MutableList to add or remove.

Can you change an element of a val array, e.g. arr[1] = "x"?

  • Yes, val only locks the variable, not the contents
  • No, val makes everything immutable
  • Only the first element
  • Only with mutableArrayOf

Answer: Yes, val only locks the variable, not the contents. val stops rebinding the variable, not mutating individual elements.

Why does Kotlin offer IntArray as well as Array<Int>?

  • IntArray can grow dynamically
  • Array<Int> is read-only
  • IntArray allows mixed types
  • IntArray stores raw primitive ints with no boxing

Answer: IntArray stores raw primitive ints with no boxing. IntArray avoids boxing, making it faster and more memory-efficient for numbers.

What does arr.size give you?

  • The last index
  • The number of elements in the array
  • The memory address
  • The element type

Answer: The number of elements in the array. size is the element count; the last valid index is size - 1.

What is the last valid index of an array of size 4?

  • 3
  • 4
  • 5
  • 0

Answer: 3. Arrays are zero-indexed, so the last index is size - 1 = 3.

What does IntArray(5) { i -> i * i } produce?

The initializer fills each slot by index i with i * i.

How should you print an array's contents to see the elements?

  • println(arr) directly
  • println(arr.toList()) or arr.joinToString()
  • println(arr.address)
  • println(arr.print())

Answer: println(arr.toList()) or arr.joinToString(). println(arr) shows a cryptic address; toList() or joinToString() shows elements.

What does arr.indices provide?

  • The element values
  • The array size as a single number
  • A reversed array
  • A range from 0 to size - 1

Answer: A range from 0 to size - 1. indices is a ready-made range of valid positions for index loops.

What is a 2D array in Kotlin?

  • A built-in Matrix type
  • An array whose elements are themselves arrays
  • A list of maps
  • Two separate arrays joined

Answer: An array whose elements are themselves arrays. A 2D array is an array of arrays, addressed as grid[row][col].

What happens if you call arr.add(x) on an array?

  • It appends the element
  • It replaces the last element
  • It won't compile; arrays are fixed-size
  • It returns a new array

Answer: It won't compile; arrays are fixed-size. Arrays have no add; use a MutableList to add elements.