To access array elements in Kotlin, you can use the indexing syntax with square brackets.
In this example,
numbers
with integer values.2
using the indexing syntax numbers[2]
.fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
val element = numbers[2]
println("Element at index 2: $element")
}
Element at index 2: 3
In this example,
fruits
with string values.fun main() {
val fruits = arrayOf("Apple", "Banana", "Orange")
for (fruit in fruits) {
println(fruit)
}
}
Apple Banana Orange
In this tutorial, we learned How to Access Array Elements in Kotlin language with well detailed examples.