To access an element in a list at a specific index in Kotlin, you can use the indexing syntax or the get()
method of the List interface.
In this example,
numbers
with integer elements.numbers[2]
.get()
method with the index as the argument: numbers.get(2)
.fun main() {
val numbers = listOf(10, 20, 30, 40, 50)
val elementAtIndex2 = numbers[2] // or numbers.get(2)
println("Element at index 2 is: $elementAtIndex2")
}
Element at index 2 is: 30
In this tutorial, we learned How to get Element in a List at a Specific Index in Kotlin language with well detailed examples.