Home »
Scala
Multi-Dimensional Array in Scala
By IncludeHelp Last updated : October 20, 2024
Multi-dimensional Array
An Array that stores data in the form multidimensional matrix. Multidimensional arrays are generally used for making matrices and tables in programming.
In Scala programming, there are two methods used to define a multidimensional array, They are :
- Array.ofDim: Scala has inbuilt ofDim() method that creates multi-dimensional array.
- Array of Array: Array of Array is used to create ragged array in Scala programming language.
Using Array.ofDim Method
Scala programming language has defined an inbuilt method ofDim() to create a multidimensional array. Using this method you can make an array upto 5-D. But to create an array with this method the exact number of rows and columns at the time of creation. The array first needs to be created first with the number of rows and columns you need and then your array is filled with values of elements.
Syntax
var arrayName = Array.ofDim[data_type](row, column)
Or
var arrayName = ofDim[data_type](row, column)
Both syntaxes are valid in Scala to create a multidimensional array.
Example
object myObject
{
def main(args: Array[String])
{
val multiArr= Array.ofDim[Int](3,2)
multiArr(0)(0) = 2
multiArr(0)(1) = 7
multiArr(1)(0) = 12
multiArr(1)(1) = 43
multiArr(2)(0) = 436
multiArr(2)(1) = 672
for(i <- 0 to 2; j <- 0 to 1){
println("Element "+ i + j + " = " + multiArr(i)(j))
}
}
}
Output
Element 00 = 2
Element 01 = 7
Element 10 = 12
Element 11 = 43
Element 20 = 436
Element 21 = 672
Code explanation
The above code is the show creation of an array using the ofDim() method. The code creates a two-dimensional array with 3 rows and 2 columns. We have passed 3,2 as an argument for this. Next, in the code, we have initialized the value of each element of the array. At last, we have used a for loop with 2 variables to print the values of the array. The print statement is like this Element ij = value.
Using Array of Array
An alternate method to create a multidimensional array. The array of array creates a rugged array. A rugged array is an array that has each contained array of different sizes. Hence, it is an elegant method to create an array of arrays. In this array, we cannot separate initialization and value feeding.
Syntax
var arrayName = Array(Array(elements), Array(elements))
This type of initialization is done keeping in mind that the array can be rugged. So, this is why we have defined an array that has arrays as its elements. Each array can have its own size. But the datatype should be the same.
Example
object myObject
{
def main(args: Array[String])
{
val multiArr= Array(Array(2,5,6),Array(12, 54,232))
for(i <- 0 to 1; j <- 0 to 2){
println("Element "+ i + j + " = " + multiArr(i)(j))
}
}
}
Output
Element 00 = 2
Element 01 = 5
Element 02 = 6
Element 10 = 12
Element 11 = 54
Element 12 = 232
Code explanation
The above code initializes a multidimensional array using an array of array. We have made an array with 2 rows and 3 columns with the help of this method, also than the number of columns for row 1 and row 2 can be different.