To access elements in a list in Kotlin, you can use indexing or iterate over the list using loops.
In this example,
numList
with integer elements.fun main() {
val numList = listOf(1, 2, 3, 4, 5)
val element = numList[2] // Accessing element at index 2
println("Element at index 2: $element")
}
Element at index 2: 3
In this example,
strList
with string elements.fun main() {
val strList = listOf("Apple", "Banana", "Cherry")
for (str in strList) {
println(str)
}
}
Apple Banana Cherry
In this tutorial, we learned How to Access Elements in a List in Kotlin language with well detailed examples.