14

不変のインデックス付き多次元配列を使用したいと思います。意味のある構造VectorVectorsのです。

scala> val v = Vector[Vector[Int]](Vector[Int](1,2,3), Vector[Int](4,5,6), Vector[Int](7,8,9))
v: scala.collection.immutable.Vector[Vector[Int]] = Vector(Vector(1, 2, 3), Vector(4, 5, 6), Vector(7, 8, 9))

のように、次元を指定するだけで空の配列を作成すると便利ですArray.ofDim

scala> a = Array.ofDim[Int](3,3)
a: Array[Array[Int]] = Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0))

ただし、Vector.ofDim関数はなく、同等のものが見つかりません。

Array.ofDim不変オブジェクトに相当するものはありますか?そうでない場合は、なぜですか?

4

3 に答える 3

20

各標準コレクション クラスには、 などのファクトリ メソッドを持つコンパニオン オブジェクトがありますfill。例:

Vector.fill(3, 3)( 0 )

関連する scaladocを参照してください。

于 2012-10-12T22:21:34.873 に答える
15

tabulateインデックスに基づいてコンテンツを設定できるという作成メソッドがあります。

scala> Vector.tabulate(3,3){ (i,j) => 3*i+j+1 }
res0: scala.collection.immutable.Vector[scala.collection.immutable.Vector[Int]] =
Vector(Vector(1, 2, 3), Vector(4, 5, 6), Vector(7, 8, 9))

ゼロ (またはその他の定数) だけが必要な場合は、fill代わりに次を使用できます。

scala> Vector.fill(3,3)(0)
res1: scala.collection.immutable.Vector[scala.collection.immutable.Vector[Int]] =
Vector(Vector(0, 0, 0), Vector(0, 0, 0), Vector(0, 0, 0))
于 2012-10-12T22:24:13.687 に答える
5

使用できますfill

scala> Vector.fill( 3 )( Vector.fill(3)(0) )
res1: scala.collection.immutable.Vector[scala.collection.immutable.Vector[Int]] = 
        Vector(Vector(0, 0, 0), Vector(0, 0, 0), Vector(0, 0, 0))
于 2012-10-12T22:15:00.133 に答える