To iterate over a Two Dimensional Array in Kotlin, you can use nested loops.
In this example,
matrix
with dimensions 3x3.fun main() {
val matrix = arrayOf(arrayOf(0, 1, 2), arrayOf(1, 2, 3), arrayOf(2, 3, 4))
for (row in matrix) {
for (element in row) {
print("$element ")
}
println()
}
}
0 1 2 1 2 3 2 3 4
In this example,
matrix
with dimensions 2x2.fun main() {
val matrix = arrayOf(arrayOf("Hello", "World"), arrayOf("Kotlin", "Java"))
for (row in matrix) {
for (element in row) {
print("$element ")
}
println()
}
}
Hello World Kotlin Java
In this tutorial, we learned How to Iterate over a Two Dimensional Array in Kotlin language with well detailed examples.