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
642 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

answered Feb 11, 2019 by Omkar
• 69,030 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
• 42,520 points
71 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 30, 2019 in Apache Spark by Gitika
• 42,520 points
187 views
0 votes
1 answer
0 votes
1 answer

Companion objects in Scala

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

answered Feb 23, 2019 in Apache Spark by Uma
148 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
• 10,950 points
6,144 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
• 10,950 points
940 views
+1 vote
11 answers

hadoop fs -put command?

put syntax: put <localSrc> <dest> copy syntax: copyF ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Aditya
39,852 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,030 points
414 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,030 points
483 views