How to create a two dimensional (2D) array in Kotlin

2 Answers

0 votes
fun main() {
    val rows: Int = 3
    val cols: Int = 4

    val arr = Array(rows, { IntArray(cols) })

    for (row in arr) {
        for (n in row) {
            print("$n ")
        }
        println()
    }
}




/*
run:

0 0 0 0 
0 0 0 0 
0 0 0 0 

*/

 



answered Feb 15, 2023 by avibootz
0 votes
import java.util.Arrays

fun main() {
    val rows: Int = 3
    val cols: Int = 4

    val arr = Array(rows, { IntArray(cols) })

    for (i in arr.indices) {
        println(arr[i].contentToString())
    }
}




/*
run:

[0, 0, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]

*/

 



answered Feb 15, 2023 by avibootz

Related questions

3 answers 208 views
3 answers 125 views
1 answer 95 views
95 views asked Jan 10, 2025 by avibootz
...