Skip to main content

Multidimensional Arrays in Kotlin

2 min read
Share:
On this page (12sections)

Introduction

Multidimensional Arrays is a fundamental concept every Kotlin developer should understand. Arrays store a fixed number of elements in memory with fast index-based access. They are useful when size is known upfront or when interoping with Java APIs.

Multidimensional arrays are arrays of arrays. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.

Definition

  • Multidimensional arrays are arrays of arrays.
  • They are useful for tables, matrices and grid data.
  • Each row can have a different length if required.

Syntax

val matrix = arrayOf(arrayOf(1,2), arrayOf(3,4))

Multidimensional Arrays in Kotlin Example Program in Kotlin

fun main(args: Array<String>) {
    val matrix = arrayOf(
        arrayOf(1, 2, 3),
        arrayOf(4, 5, 6)
    )
    println("Value at [1][2]: ${matrix[1][2]}")
}

Sample Output

Value at [1][2]: 6

When to use

Use arrays when you need fixed-size storage, primitive arrays without boxing overhead, or compatibility with Java vararg APIs.

How it works

  1. The program starts with a main function — the entry point that runs when you execute the file.

  2. val matrix = arrayOf( assigns or updates a value used later in the program.

  3. The println("Value at [1][2]: ${matrix[1][2]}") statement writes a line to the console — this produces part of the sample output below.

  4. Multidimensional arrays are arrays of arrays.

  5. Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (kotlinc / kotlin). Compare your console output with the sample output shown below.

Best Practices

  • Prefer listOf or arrayOf factory functions over manual size allocation when possible.
  • Use typed arrays (IntArray) for numeric data to avoid boxing overhead.
  • Bounds-check mentally: invalid indices throw IndexOutOfBoundsException.

Common Mistakes

  • Confusing Array<Int> (boxed) with IntArray (unboxed primitives).
  • Assuming arrays are dynamically resizable like ArrayList — they are fixed size.
  • Using reference equality (===) when comparing array contents — use contentEquals.

Key Points

  • Multidimensional arrays are arrays of arrays.
  • They are useful for tables, matrices and grid data.
  • Each row can have a different length if required.
  • Test the example locally and verify the output matches the sample.
  • Experiment by changing input values to see how behaviour changes.

Notes

  • Semicolons at the end of statements are optional in Kotlin.

Frequently Asked Questions

What is Multidimensional Arrays in Kotlin?
Multidimensional arrays are arrays of arrays.
When should I use Multidimensional Arrays?
Use arrays when you need fixed-size storage, primitive arrays without boxing overhead, or compatibility with Java vararg APIs.
How is Multidimensional Arrays different from Java?
Each row can have a different length if required.
How do I practice this topic?
Copy the example program into IntelliJ IDEA or Android Studio, run it, then modify values or add print statements to confirm your understanding.

Related Tutorials

Search tutorials