scala语法 -多维数组

版权声明:本文为博主原创文章,转载联系 [email protected] https://blog.csdn.net/qq_31573519/article/details/83117859

不多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

猜你喜欢

转载自blog.csdn.net/qq_31573519/article/details/83117859