Category : Scala | Sub Category : Scala Programs | By Prasad Bonam Last updated: 2020-10-08 10:27:39 Viewed : 842
Scala multi dimensionalArray
·
Syntax for Multidimensional Array
var arrayName = Array.ofDim[ArrayType](NoOfRows,NoOfColumns) or
var arrayName = Array(Array(element...), Array(element...), ...)
Example 3:
Following example
illustrates about Scala multi dimensional Array
Save the file as − MultiArrayDec.scala
MultiArrayDec.scala
package runnerdev
object MulitDimeArrayEx1 {
def main(args: Array[String]) {
var arrayObj = new MultiArrayDec()
arrayObj.show()
}
}
class MultiArrayDec {
var multiArray = Array(Array(1, "cat", "One", 4, 5), Array(6, 7, "dog", 9, 10)) // Creating
multidimensional array
def show() {
for (x <- 0 to 1) { // Traversing
elements using loop
for (y <- 0 to 4) {
print(" " + multiArray(x)(y))
}
println()
}
}
}
compile
and run the above example as follows
scala> scalac MultiArrayDec.scala
scala> scala MultiArrayDec
Output :
1 cat One 4 5
6 7 dog 9 10
Example 3:
Following example
illustrates about Scala multi dimensional Array(ofDim)
Save the file as − MulitDimeArrayEx2.scala
MulitDimeArrayEx2.scala
package runnerdev
object MulitDimeArrayEx2 {
def main(args: Array[String]) {
var arrayObj = new MultiArrayDec1()
arrayObj.show()
}
}
class MultiArrayDec1 {
var arr = Array.ofDim[Int](2, 2) // Creating
multidimensional array
arr(1)(0) = 10 // Assigning value
arr(1)(0) = 11 // Assigning value
arr(1)(1) = 99 // Assigning value
def show() {
for (x <- 0 to 1) { // Traversing
elements by using loop
for (y <- 0 to 1) {
print(" " + arr(x)(y))
}
println()
}
println("Third Element
= "
+ arr(1)(1)) // Accessing
elements by using index
}
}
compile
and run the above example as follows
scala> scalac MultiArrayDec1.scala
scala> scala MultiArrayDec1
Output :
Output:
0 0
11 99
Third Element = 99
In Scala, you can create and work with multi-dimensional arrays by using arrays of arrays or by using collections like ArrayBuffer
nested within other collections. Here are examples of both approaches for creating multi-dimensional arrays:
Using Arrays of Arrays:
scala// Creating a 2D array of integers val matrix: Array[Array[Int]] = Array.ofDim[Int](3, 3) // Initializing the elements matrix(0)(0) = 1 matrix(0)(1) = 2 matrix(0)(2) = 3 matrix(1)(0) = 4 matrix(1)(1) = 5 matrix(1)(2) = 6 matrix(2)(0) = 7 matrix(2)(1) = 8 matrix(2)(2) = 9 // Accessing elements val element = matrix(1)(2) // Accesses the element at row 1, column 2 (equals 6) // Printing the matrix for (row <- matrix) { for (elem <- row) { print(s"$elem ") } println() }
In this example, we create a 2D array matrix
and initialize its elements. We access and print the elements using nested loops.
Using Collections (ArrayBuffer) within Collections:
scalaimport scala.collection.mutable.ArrayBuffer // Creating a 2D collection using ArrayBuffer val matrix: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer.fill(3)(ArrayBuffer.fill(3)(0)) // Initializing the elements matrix(0)(0) = 1 matrix(0)(1) = 2 matrix(0)(2) = 3 matrix(1)(0) = 4 matrix(1)(1) = 5 matrix(1)(2) = 6 matrix(2)(0) = 7 matrix(2)(1) = 8 matrix(2)(2) = 9 // Accessing elements val element = matrix(1)(2) // Accesses the element at row 1, column 2 (equals 6) // Printing the matrix for (row <- matrix) { for (elem <- row) { print(s"$elem ") } println() }
In this example, we use ArrayBuffer
to create a 2D collection matrix
and initialize its elements. The access and printing of elements are done similarly to the previous example.
Both approaches allow you to work with multi-dimensional data structures in Scala. The choice between arrays of arrays and collections like ArrayBuffer
depends on your specific use case and requirements. Collections like ArrayBuffer
offer more flexibility in terms of size changes and are often preferred for more dynamic scenarios.
Here are examples of creating and working with multi-dimensional arrays in Scala using both arrays of arrays and collections like ArrayBuffer
, along with their respective outputs.
Using Arrays of Arrays:
scala// Creating a 2D array of integers val matrix: Array[Array[Int]] = Array.ofDim[Int](3, 3) // Initializing the elements matrix(0)(0) = 1 matrix(0)(1) = 2 matrix(0)(2) = 3 matrix(1)(0) = 4 matrix(1)(1) = 5 matrix(1)(2) = 6 matrix(2)(0) = 7 matrix(2)(1) = 8 matrix(2)(2) = 9 // Accessing elements val element = matrix(1)(2) // Accesses the element at row 1, column 2 (equals 6) // Printing the matrix for (row <- matrix) { for (elem <- row) { print(s"$elem ") } println() }
Output:
1 2 3 4 5 6 7 8 9
In this example, we create a 2D array matrix
, initialize its elements, access an element, and print the entire matrix.
Using Collections (ArrayBuffer) within Collections:
scalaimport scala.collection.mutable.ArrayBuffer // Creating a 2D collection using ArrayBuffer val matrix: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer.fill(3)(ArrayBuffer.fill(3)(0)) // Initializing the elements matrix(0)(0) = 1 matrix(0)(1) = 2 matrix(0)(2) = 3 matrix(1)(0) = 4 matrix(1)(1) = 5 matrix(1)(2) = 6 matrix(2)(0) = 7 matrix(2)(1) = 8 matrix(2)(2) = 9 // Accessing elements val element = matrix(1)(2) // Accesses the element at row 1, column 2 (equals 6) // Printing the matrix for (row <- matrix) { for (elem <- row) { print(s"$elem ") } println() }
Output:
1 2 3 4 5 6 7 8 9
This second example using ArrayBuffer
produces the same output as the first one with arrays of arrays. It demonstrates how to create, initialize, access, and print a 2D collection using ArrayBuffer
.
Both examples show how to work with 2D arrays or collections in Scala. The choice between arrays of arrays and collections depends on your specific needs and requirements.