Scala Tutorial
Basics
Control Statements
OOP Concepts
Parameterized - Type
Exceptions
Scala Annotation
Methods
String
Scala Packages
Scala Trait
Collections
Scala Options
Miscellaneous Topics
In Scala, you can create and work with multidimensional arrays using the Array
data structure. While Scala does not have built-in syntax for multidimensional arrays like some other languages, you can achieve the same effect by creating arrays of arrays.
Here's how you can work with multidimensional arrays in Scala:
To create a 2D array (an array of arrays), you can do:
val matrix: Array[Array[Int]] = Array.ofDim[Int](3, 3)
This initializes a 3x3 matrix of integers, filled with zeros.
To access or modify an element in a multidimensional array, you simply chain the index access:
// Setting a value matrix(0)(0) = 1 matrix(1)(1) = 5 matrix(2)(2) = 9 // Getting a value val element = matrix(1)(1) // element would be 5
You can use nested loops to iterate over a multidimensional array:
for (i <- matrix.indices) { for (j <- matrix(i).indices) { println(s"Element at ($i,$j) = ${matrix(i)(j)}") } }
Or, to make it more idiomatic in Scala, you can use a for-comprehension:
for { i <- matrix.indices j <- matrix(i).indices } println(s"Element at ($i,$j) = ${matrix(i)(j)}")
You can apply higher-order functions like map
to the multidimensional array:
val doubledMatrix = matrix.map(row => row.map(_ * 2))
This will create a new matrix with all elements doubled.
You can also create arrays that don't have uniform dimensions:
val jaggedArray: Array[Array[Int]] = Array( Array(1), Array(2, 3), Array(4, 5, 6) )
This is sometimes referred to as a "jagged array" because it doesn't form a perfect rectangle.
While multidimensional arrays in Scala are essentially arrays of arrays, Scala provides various utilities, like the ofDim
method and a rich set of collection functions, to make it easy to work with them. Remember, though, that multidimensional arrays in Scala are mutable. If you're aiming to maintain the principles of functional programming, consider using other data structures or libraries that provide immutable multidimensional collections.
Creating and initializing multidimensional arrays in Scala:
Array.ofDim
method, specifying the dimensions.val twoDArray: Array[Array[Int]] = Array.ofDim[Int](3, 4) twoDArray(0)(0) = 1
Accessing elements in Scala multidimensional arrays:
val value: Int = twoDArray(1)(2)
Iterating over multidimensional arrays in Scala:
for { i <- 0 until 3 j <- 0 until 4 } yield println(s"Element at ($i, $j): ${twoDArray(i)(j)}")
Resizable multidimensional arrays in Scala:
ArrayBuffer
in Scala provides a resizable alternative to traditional arrays.import scala.collection.mutable.ArrayBuffer val resizableArray: ArrayBuffer[ArrayBuffer[Int]] = ArrayBuffer.fill(3, 4)(0) resizableArray(1)(2) = 42
Nested arrays vs. true multidimensional arrays in Scala:
// True Multidimensional Array val trueMultiArray: Array[Array[Int]] = Array.ofDim[Int](3, 4) // Nested Arrays (Array of Arrays) val nestedArrays: Array[Array[Int]] = Array.fill(3)(Array.fill(4)(0))
Scala Array of Arrays concept:
val arrayOfArrays: Array[Array[Int]] = Array( Array(1, 2, 3), Array(4, 5, 6), Array(7, 8, 9) )
Using matrices with multidimensional arrays in Scala:
val matrix: Array[Array[Double]] = Array.ofDim[Double](2, 2) matrix(0)(0) = 1.0
Common operations on multidimensional arrays in Scala:
val matrix1: Array[Array[Int]] = Array(Array(1, 2), Array(3, 4)) val matrix2: Array[Array[Int]] = Array(Array(5, 6), Array(7, 8)) val sumMatrix: Array[Array[Int]] = matrix1.zip(matrix2).map { case (row1, row2) => row1.zip(row2).map { case (a, b) => a + b } }