Multidimensional Array in Scala

0 votes

Can you please explain me multidimensional array in Scala with examples?

Feb 11, 2019 in Apache Spark by Sowmya
1,924 views

1 answer to this question.

0 votes

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.

Multidimensional Array Syntax

var arrayName = Array.ofDim[ArrayType](NoOfRows,NoOfColumns) or
var arrayName = Array(Array(element?), Array(element?), ?)

Scala Multidimensional Array Example by using ofDim

In This example, we have created array by using ofDim method.

class ArrayExample{

    var arr = Array.ofDim[Int](2,2) // Creating multidimensional array

    arr(1)(0) = 15 // Assigning value

    def show(){

        for(i<- 0 to 1){ // Traversing elements by using loop

           for(j<- 0 to 1){

                print(" "+arr(i)(j))

            }

            println()

        }

        println("Third Element = "+ arr(1)(1)) // Accessing elements by using index

    }

}

object MainObject{

    def main(args:Array[String]){

        var a = new ArrayExample()

        a.show()

    }

}

Output:

0 0

15 0

Third Element = 0

Scala Multidimensional Array by using Array of Array

Apart from ofDim you can also create multidimensional array by using array of array. In this example, we have created multidimensional array by using array of array.

class ArrayExample{

    var arr = Array(Array(1,2,3,4,5), Array(6,7,8,9,10)) // Creating multidimensional array

    def show(){

        for(i<- 0 to 1){ // Traversing elements using loop

           for(j<- 0 to 4){

                print(" "+arr(i)(j))

            }

            println()

        }

    }

}

object MainObject{

    def main(args:Array[String]){

        var a = new ArrayExample()

        a.show()

    }

}

Output:

1 2 3 4 5
6 7 8 9 10

Hope this helps!

If you need to know more about Scala, join Spark course today and become the expert.

Thanks!!

answered Feb 11, 2019 by Omkar
• 69,220 points

Related Questions In Apache Spark

0 votes
1 answer

How to process an Array in Scala?

Hi, We can use loop control structures to ...READ MORE

answered Jul 25, 2019 in Apache Spark by Gitika
• 65,770 points
641 views
0 votes
1 answer

How to work with multidimensional arrays in Scala?

Hi, Here is an example you can follow: scala> ...READ MORE

answered Jul 31, 2019 in Apache Spark by Gitika
• 65,770 points
951 views
0 votes
1 answer
0 votes
1 answer

Companion objects in Scala

When a singleton object is named the ...READ MORE

answered Feb 24, 2019 in Apache Spark by Uma
1,067 views
+1 vote
2 answers
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
11,028 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
2,535 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
108,830 views
0 votes
1 answer

where can i get spark-terasort.jar and not .scala file, to do spark terasort in windows.

Hi! I found 2 links on github where ...READ MORE

answered Feb 13, 2019 in Apache Spark by Omkar
• 69,220 points
1,341 views
–1 vote
1 answer

Not able to use sc in spark shell

Seems like master and worker are not ...READ MORE

answered Jan 3, 2019 in Apache Spark by Omkar
• 69,220 points
1,759 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP