Multidimensional Arrays in Scala

Multidimensional Array is basically an array storing the data in matrix format. For, matrices and table we need multidimensional array. We can create multidimensional array by using Array.ofDim and Array of Array method.
 
Syntax

val arrayname = Array.ofDim[data_type](number of rows, number of cols)
or
var arrayname = Array(Array(elements), Array(elements))

 

Multidimensional array by using Array.ofDim


Scala has a method Array.ofDim to create a multidimensional array. This approach can be used to create arrays of up to five dimensions. Required we need to know the number of rows and columns at creation time. After declaring the array, we add elements to it .
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala Program of Multidimensional array 
// using Array.ofDim
object MultiArr
{
    def main(args: Array[String])
    {
        //creating the array of 2 rows and 2 columns
        val mymultiarr= Array.ofDim[Int](2, 2
      
        //Assigning the values
        mymultiarr(0)(0) = 2                 
        mymultiarr(0)(1) = 7
        mymultiarr(1)(0) = 3
        mymultiarr(1)(1) = 4
      
        for(i<-0 to 1; j<-0 until 2)
        {
            print(i, j)
          
           //Accessing the elements
           println("=" + mymultiarr(i)(j))     
        }
    }
}

chevron_right


Output

(0,0)=2
(0,1)=7
(1,0)=3
(1,1)=4

 

Multidimensional Array by using Array of Array


This gives more control of the process and lets us create “ragged” arrays (where each contained array may be a different size). We can also create a multidimensional array by using Array of Array. In the example below, we have created a multidimensional array using Array of Array.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala Program of Multidimensional array 
// using Array of Array
object MultiArr
{
    def main(args: Array[String])
    {
        // Creating and assigning the values 
        // to the array
        var arr= Array(Array(0, 2, 4, 6, 8),
                    Array(1, 3, 5, 7, 9)) 
  
        for(i<-0 to 1)
        {
            for(j<- 0 to 4)
            {
                // Accessing the values
                print(" "+arr(i)(j)) 
            }
            println()
        }
    }
}

chevron_right


Output:

 0 2 4 6 8
 1 3 5 7 9

Now, let us consider an example showing the element with (row, column)
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala Program of Multidimensional array 
// using Array of Array
object Mad
{
    def main(args: Array[String])
    {
      
        // Creating and assigning the values to the array
        var arr= Array(Array(0, 2, 4, 6, 8), 
                    Array(1, 3, 5, 7, 9)) 
      
        for(i<-0 to 1; j<-0 until 5)
        {
            print(i, j)
              
            //Accessing the elements
            println("=" + arr(i)(j)) 
        }
    }
}

chevron_right


Output:

(0,0)=0
(0,1)=2
(0,2)=4
(0,3)=6
(0,4)=8
(1,0)=1
(1,1)=3
(1,2)=5
(1,3)=7
(1,4)=9


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.