Accessing Array Elements in Kotlin
On this page (12sections)
Introduction
Accessing Array Elements 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.
Elements are accessed using the index operator [ ]. 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
- Elements are accessed using the index operator [ ].
- Attempting to access an invalid index throws ArrayIndexOutOfBoundsException.
- Arrays also provide first(), last() and get/set methods.
Syntax
val value = array[index]
Accessing Array Elements in Kotlin Example Program in Kotlin
fun main(args: Array<String>) {
val marks = arrayOf(85, 90, 78)
println("Second mark: ${marks[1]}")
marks[1] = 92
println("Updated second mark: ${marks[1]}")
}
Sample Output
Second mark: 90
Updated second mark: 92
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
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
val marks = arrayOf(85, 90, 78)assigns or updates a value used later in the program. -
The
println("Second mark: ${marks[1]}")statement writes a line to the console — this produces part of the sample output below. -
marks[1] = 92assigns or updates a value used later in the program. -
The
println("Updated second mark: ${marks[1]}")statement writes a line to the console — this produces part of the sample output below. -
Elements are accessed using the index operator [ ].
-
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
listOforarrayOffactory 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) withIntArray(unboxed primitives). - Assuming arrays are dynamically resizable like ArrayList — they are fixed size.
- Using reference equality (
===) when comparing array contents — usecontentEquals.
Key Points
- Elements are accessed using the index operator [ ].
- Attempting to access an invalid index throws ArrayIndexOutOfBoundsException.
- Arrays also provide first(), last() and get/set methods.
- 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 Accessing Array Elements in Kotlin?
When should I use Accessing Array Elements?
How is Accessing Array Elements different from Java?
How do I practice this topic?
Related Tutorials
Array Initialization in Kotlin
Learn Array Initialization in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialArray Iteration in Kotlin
Learn Array Iteration in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial