How to print two dimensional (2D) array in Scala

1 Answer

0 votes
object MyClass {
    def main(args: Array[String]): Unit = {
        val arr = Array.ofDim[Int](4, 3);

        val row1 = arr.apply(0);
        row1.update(0, 11);
        row1.update(1, 22);
        row1.update(2, 33);
    
        val row2 = arr.apply(1);
        row2.update(0, 88);
        row2.update(1, 99);
    
        val row3 = arr.apply(2);
        row3.update(0, 77);
    
        for (row <- arr) {
            for (n <- row) {
                print(n);
                print(" ");
            }
            println();
        }
    }
}
    
    
    
    
/*
run:
    
11 22 33 
88 99 0 
77 0 0 
0 0 0 
 
*/

 



answered Jun 1, 2021 by avibootz

Related questions

2 answers 173 views
1 answer 164 views
2 answers 199 views
1 answer 153 views
1 answer 104 views
1 answer 84 views
...