scala語法 -多維陣列

止魚發表於2018-10-17

不多BB,上程式碼

  def testMatrixA () = {
    val matrix = Array.ofDim[Int](3, 4)
    printMatrix(matrix)
  }
  
  def printMatrix(matrix: Array[Array[Int]]): Unit ={
    // 建立矩陣
    for (i <- 0 to 2) {
      for ( j <- 0 to 3) {
        matrix(i)(j) = j;
      }
    }
    // 列印二維陣列
    for (i <- 0 to 2) {
      for ( j <- 0 to 3) {
        print(" " + matrix(i)(j));
      }
      println();
    }
  }

執行 testMatrix 方法結果:

 0 1 2 3
 0 1 2 3
 0 1 2 3

相關文章