「Scala入門」多次元配列のサンプル

書式
val 多次元配列名 = Array.ofDim[Int](3, 3)

var cftA = Array.ofDim[Int](10, 9)
var cftB = Array.ofDim[Int](2, 3, 4
n次元配列は配列の配列として扱います。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import Array._
object Cftest {
def main(args: Array[String]) {
var tMat = ofDim[Int](3,3)
// マトリックスを作成
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
tMat(i)(j) = j;
}
}
// 2次元配列を出力
for (i <- 0 to 2) {
for ( j <- 0 to 2) {
print(" " + tMat(i)(j));
}
println();
}
}
}
import Array._ object Cftest { def main(args: Array[String]) { var tMat = ofDim[Int](3,3) // マトリックスを作成 for (i <- 0 to 2) { for ( j <- 0 to 2) { tMat(i)(j) = j; } } // 2次元配列を出力 for (i <- 0 to 2) { for ( j <- 0 to 2) { print(" " + tMat(i)(j)); } println(); } } }
import Array._

object Cftest {
   def main(args: Array[String]) {
      var tMat = ofDim[Int](3,3)
      
      // マトリックスを作成
      for (i <- 0 to 2) {
         for ( j <- 0 to 2) {
            tMat(i)(j) = j;
         }
      }
      
      // 2次元配列を出力
      for (i <- 0 to 2) {
         for ( j <- 0 to 2) {
            print(" " + tMat(i)(j));
         }
         println();
      }
   }
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ scalac Cftest.scala
$ scala Cftest
0 1 2
0 1 2
0 1 2
$ scalac Cftest.scala $ scala Cftest 0 1 2 0 1 2 0 1 2
$ scalac Cftest.scala
$ scala Cftest
0 1 2
0 1 2
0 1 2

 

Scala

Posted by arkgame