Multidimensional Array in Scala

0 votes

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

Feb 11 in Apache Spark by Sowmya
108 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 by Omkar
• 67,380 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 in Apache Spark by Gitika
• 25,340 points
17 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 in Apache Spark by Gitika
• 25,340 points
30 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 in Apache Spark by Uma
40 views
0 votes
1 answer
0 votes
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,670 points
2,684 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,670 points
279 views
0 votes
10 answers

hadoop fs -put command?

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

answered Dec 7, 2018 in Big Data Hadoop by Aditya
13,327 views
0 votes
1 answer
0 votes
1 answer

Not able to use sc in spark shell

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

answered Jan 3 in Apache Spark by Omkar
• 67,380 points
135 views