Multidimensional Arrays in Scala
Last Updated :
11 Apr, 2023
In Scala, multidimensional arrays can be represented as arrays of arrays, where each nested array represents a row or a column. The size of the array can be defined using two integer values representing the number of rows and columns, respectively.
Here's an example code with output that demonstrates the usage of multidimensional arrays in Scala:
Scala
object MultiDimArrayExample {
def main(args: Array[String]): Unit = {
// Create a 2D array of integers with 3 rows and 4 columns
val arr: Array[Array[Int]] = Array.ofDim[Int](3, 4)
// Initialize the elements of the array
for (i <- 0 until 3; j <- 0 until 4) {
arr(i)(j) = i + j
}
// Access and print the elements of the array
println("Printing elements of 2D array:")
for (i <- 0 until 3) {
for (j <- 0 until 4) {
print(arr(i)(j) + " ")
}
println()
}
// Create a jagged array of integers with 3 rows and varying number of columns
val jaggedArr: Array[Array[Int]] = Array.ofDim[Int](3, 0)
jaggedArr(0) = Array(1, 2, 3)
jaggedArr(1) = Array(4, 5)
jaggedArr(2) = Array(6, 7, 8, 9)
// Access and print the elements of the jagged array
println("Printing elements of jagged array:")
for (i <- 0 until 3) {
for (j <- 0 until jaggedArr(i).length) {
print(jaggedArr(i)(j) + " ")
}
println()
}
}
}
OutputPrinting elements of 2D array:
0 1 2 3
1 2 3 4
2 3 4 5
Printing elements of jagged array:
1 2 3
4 5
6 7 8 9
In this example, we first create a 2D array arr with 3 rows and 4 columns using Array.ofDim(), and initialize the elements using a nested loop. Then, we print the elements of the 2D array using a nested loop.
Next, we create a jagged array jaggedArr with 3 rows and varying number of columns by initializing each row with a different array. Finally, we access and print the elements of the jagged array using a nested loop with a varying upper bound based on the length of each row.
Multidimensional Array is basically an array storing the data in matrix format. For, matrices and table we need multidimensional array. We can create multidimensional array by using Array.ofDim and Array of Array method. Syntax
val arrayname = Array.ofDim[data_type](number of rows, number of cols) or var arrayname = Array(Array(elements), Array(elements))
Multidimensional array by using Array.ofDim
Scala has a method Array.ofDim to create a multidimensional array. This approach can be used to create arrays of up to five dimensions. Required we need to know the number of rows and columns at creation time. After declaring the array, we add elements to it . Example:
Scala
// Scala Program of Multidimensional array
// using Array.ofDim
object MultiArr
{
def main(args: Array[String])
{
//creating the array of 2 rows and 2 columns
val mymultiarr= Array.ofDim[Int](2, 2)
//Assigning the values
mymultiarr(0)(0) = 2
mymultiarr(0)(1) = 7
mymultiarr(1)(0) = 3
mymultiarr(1)(1) = 4
for(i<-0 to 1; j<-0 until 2)
{
print(i, j)
//Accessing the elements
println("=" + mymultiarr(i)(j))
}
}
}
Output
(0,0)=2
(0,1)=7
(1,0)=3
(1,1)=4
Multidimensional Array by using Array of Array
This gives more control of the process and lets us create “ragged” arrays (where each contained array may be a different size). We can also create a multidimensional array by using Array of Array. In the example below, we have created a multidimensional array using Array of Array. Example:
Scala
// Scala Program of Multidimensional array
// using Array of Array
object MultiArr
{
def main(args: Array[String])
{
// Creating and assigning the values
// to the array
var arr= Array(Array(0, 2, 4, 6, 8),
Array(1, 3, 5, 7, 9))
for(i<-0 to 1)
{
for(j<- 0 to 4)
{
// Accessing the values
print(" "+arr(i)(j))
}
println()
}
}
}
Output:
0 2 4 6 8
1 3 5 7 9
Now, let us consider an example showing the element with (row, column) Example:
Scala
// Scala Program of Multidimensional array
// using Array of Array
object Mad
{
def main(args: Array[String])
{
// Creating and assigning the values to the array
var arr= Array(Array(0, 2, 4, 6, 8),
Array(1, 3, 5, 7, 9))
for(i<-0 to 1; j<-0 until 5)
{
print(i, j)
//Accessing the elements
println("=" + arr(i)(j))
}
}
}
Output:
(0,0)=0
(0,1)=2
(0,2)=4
(0,3)=6
(0,4)=8
(1,0)=1
(1,1)=3
(1,2)=5
(1,3)=7
(1,4)=9
Similar Reads
Multidimensional Array in R
Arrays are the R data objects which can store data in more than two dimensions. For example: If we create an array of dimensions (2, 3, 4) then it creates 4 rectangular matrices each with 2 rows and 3 columns. These types of arrays are called Multidimensional Arrays. Arrays can store only data types
3 min read
Java Multi-Dimensional Arrays
Multidimensional arrays are used to store the data in rows and columns, where each row can represent another individual array are multidimensional array. It is also known as array of arrays. The multidimensional array has more than one dimension, where each row is stored in the heap independently. T
10 min read
Scala | Arrays
Array is a special kind of collection in scala. it is a fixed size data structure that stores elements of the same data type. The index of the first element of an array is zero and the last element is the total number of elements minus one. It is a collection of mutable values. It corresponds to arr
6 min read
Methods to call on a Set in Scala
A set is a collection that only contains unique items. In Scala, both mutable and immutable sets are available. The mutable set is those set in which the value of the object is change, but, in the immutable set, the value of the object is not changed itself. The immutable set is defined under Scala.
13 min read
Monads in Scala
In Scala, Monads is a construction which performs successive calculations. It is an object which covers the other object. It is worth noting that here, the output of an operation at some step is an input to another computations, which is a parent to the recent step of the program stated. Monad is ne
4 min read
ListMap in Scala
Immutable maps Implemented by using a list-based data structure. The Scala List class holds a sequenced, linear list of items. We must import scala.collection.mutable.ListMap for ListMap. ListMap collection used only for a small number of elements.Syntax: var listMapName = ListMap("k1"->"v1", "k2
3 min read
Scala | ArrayBuffer
Array in scala is homogeneous and mutable, i.e it contains elements of the same data type and its elements can change but the size of array size canât change. To create a mutable, indexed sequence whose size can change ArrayBuffer class is used. To use, ArrayBuffer, scala.collection.mutable.ArrayBuf
4 min read
Interesting fact about Scala
Scala(pronounced as "skah-lah") is general-purpose programming language designed by Martin Odersky. The design of Scala started in 2001 at EPFL, Lausanne, Switzerland. Scala was released publicly in 2004 on Java platform. Scala is designed to be concise and addresses criticisms of Java. Scala source
3 min read
How to create partition in scala?
In the world of big data, processing efficiency is key, and data partitioning emerges as a vital tool for optimizing performance. By strategically dividing large datasets into smaller subsets, partitioning enables parallel processing, significantly accelerating data manipulation tasks. In Scala, ach
2 min read
How to split Array in Scala?
Arrays are a fundamental data structure in Scala, used to store collections of elements of the same type. Splitting an array involves dividing it into smaller sub-arrays based on specific criteria. This article explores different methods for achieving this in Scala. Table of Content 1. Using `splitA
3 min read