Multidimensional Array in Scala

0 votes

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

Feb 11 in Apache Spark by Sowmya
59 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,120 points

Related Questions In Apache Spark

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
32 views
0 votes
1 answer

what are the job optimization Technics in spark and scala ?

There are different methods to achieve optimization ...READ MORE

answered Mar 18 in Apache Spark by Veer
147 views
0 votes
1 answer

Scala: Add user input to array

You can try this:  object printarray { ...READ MORE

answered Jun 19 in Apache Spark by Dinesha
19 views
0 votes
0 answers
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,030 points
2,038 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,030 points
192 views
0 votes
10 answers

hadoop fs -put command?

copy command can be used to copy files ...READ MORE

answered Dec 7, 2018 in Big Data Hadoop by Sujay
10,377 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,120 points
100 views