How to get matrix size in Kotlin

1 Answer

0 votes
fun main() {
    val matrix = arrayOf(
        arrayOf(2, 0, 5, 9),
        arrayOf(0, 4, 0, 3),
        arrayOf(0, 8, 0, 1)
    )

    // Get the number of rows
    val rows = matrix.size

    // Get the number of columns
    val columns = matrix[0].size

    val totalCells = rows * columns

    println("Matrix size: $rows rows x $columns columns")
    println("Total Cells: $totalCells")
}



/*
run:

Matrix size: 3 rows x 4 columns
Total Cells: 12

*/

 



answered Oct 2, 2025 by avibootz
...