scala dynamic multi dimensional mutable arrays lik

2020-05-27 04:58发布

问题:

Is there any way to build dynamic multi-dimensional arrays in Scala? I know arrays in Scala must be initialized in its sizes and dimensions, so I don't want that. The data structure should be dynamic. I tried to build it with lists in lists, but I lost myself some way.

There are so many different types, maybe I just didn't find the right one. So please push me to the right direction.

回答1:

If you want to do something like

a(5) = // result of some computation

then you'll need to use something from the mutable collections hierarchy. I'd suggest ArrayBuffer.

scala> import scala.collection.mutable.ArrayBuffer
import scala.collection.mutable.ArrayBuffer

scala> val a = ArrayBuffer.fill(3,3)(0)
a: scala.collection.mutable.ArrayBuffer[scala.collection.mutable.ArrayBuffer[Int]] = ArrayBuffer(ArrayBuffer(0, 0, 0), ArrayBuffer(0, 0, 0), ArrayBuffer(0, 0, 0))

scala> a(2)(1) = 4

scala> a(0) = ArrayBuffer(1,2,3)

scala> a
res2: scala.collection.mutable.ArrayBuffer[scala.collection.mutable.ArrayBuffer[Int]] = ArrayBuffer(ArrayBuffer(1, 2, 3), ArrayBuffer(0, 0, 0), ArrayBuffer(0, 4, 0))

Note that fill lets you automatically create and initialize up to 5D structures. Note also that you can extend the length of these, but it won't extend the entire multidimensional structure, just the one you add to. So, for example,

scala> a(2) += 7 // Add one element to the end of the array
res3: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(0, 4, 0, 7)

scala> a
res4: scala.collection.mutable.ArrayBuffer[scala.collection.mutable.ArrayBuffer[Int]]
= ArrayBuffer(ArrayBuffer(1, 2, 3), ArrayBuffer(0, 0, 0), ArrayBuffer(0, 4, 0, 7))


回答2:

Well, it depends a lot on what you intend to do with it, but your best guess is IndexedSeq[IndexedSeq[T]] (or deeper nestings), using Vector as the implementation for IndexedSeq (it's the default implementation anyway).

For example:

scala> IndexedSeq(IndexedSeq(1, 2, 3), IndexedSeq(4, 5), IndexedSeq(6, 7, 8, 9)) res0: IndexedSeq[IndexedSeq[Int]] = Vector(Vector(1, 2, 3), Vector(4, 5), Vector(6, 7, 8, 9))



回答3:

You may create an array of 2 dims dynamically like this:

val aa : Array[Array[Int]] = Array.ofDim (3, 4)

Well, yes, I see, the size is fixed. How about that:

val i = random.nextInt (5) + 1
val j = new GregorianCalendar (). get (Calendar.DAY_OF_WEEK)
val aa : Array[Array[Int]] = Array.ofDim (i, j)

Yes, it is bound to two dimensions. How would you use an array of previously unknown dimension?

Well - at least, you can:

val aa : Array [Int] = Array.ofDim (2)

aa: Array[Int] = Array(0, 0)

val aaa = Array.fill (3) (aa)         

aaa: Array[Array[Int]] = Array(Array(0, 0), Array(0, 0), Array(0, 0))



标签: scala types