Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala dynamic multi dimensional mutable arrays like datastructures

Tags:

types

scala

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.

like image 830
evildead Avatar asked Aug 19 '10 23:08

evildead


People also ask

Are arrays mutable in Scala?

Array in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size can't change. To create a mutable, indexed sequence whose size can change ArrayBuffer class is used. To use, ArrayBuffer, scala. collection.

What is multidimensional array in Scala?

Multidimensional array is an array which store data in matrix form. You can create from two dimensional to three, four and many more dimensional array according to your need. Below we have mentioned array syntax. Scala provides an ofDim method to create multidimensional array.

Can a multidimensional array have different data types?

You can create an array with elements of different data types when declare the array as Object. Since System. Object is the base class of all other types, an item in an array of Objects can have a reference to any other type of object.

Can multi-dimensional arrays be indexed?

Indexing multi-dimensional arraysMulti-dimensional arrays are indexed in GAUSS the same way that matrices are indexed, using square brackets [] . Scanning above, you can see that the value of the element at the intersection of the third row and second column of x1 is 8.


1 Answers

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))
like image 183
Rex Kerr Avatar answered Sep 28 '22 03:09

Rex Kerr