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