Multidimensional Array in Scala

0 votes

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

Feb 11 in Apache Spark by Sowmya
34 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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
• 65,850 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
23 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
83 views
0 votes
1 answer

Changing Column position in spark dataframe

Yes, you can reorder the dataframe elements. You need ...READ MORE

answered Apr 19, 2018 in Apache Spark by Ashish
• 2,630 points
2,662 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
• 9,070 points
1,679 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
• 9,070 points
132 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
8,154 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
• 65,850 points
72 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.