To iterate over an array in Kotlin, you can use the for
loop or forEach
function.
In this example,
numbers
with some values.for
loop to iterate over each element of the array and print the elements.fun main() {
val numbers = intArrayOf(1, 2, 3, 4, 5)
for (number in numbers) {
println(number)
}
}
1 2 3 4 5
In this example,
names
with some strings.forEach
function to iterate over each element of the array and print the elements.fun main() {
val names = arrayOf("Alice", "Bob", "Charlie")
names.forEach { println(it) }
}
Alice Bob Charlie
In this tutorial, we learned How to Iterate Over an Array in Kotlin language with well detailed examples.