Exploring Multidimensional Arrays in Scala

Topic Overview

Welcome to today's session on "Multidimensional Arrays and Their Traversal in Scala." In Scala, we can use arrays to hold multiple arrays at each index, akin to having nested collections. You can imagine these arrays as an "apartment building," where each floor corresponds to an outer array and each apartment on a floor corresponds to an inner array. Our aim is to build your foundational skillset in handling these "apartment complexes" efficiently in Scala.

Creating Multidimensional Arrays

In Scala, a multidimensional array is created using nested Array structures. Here is an example of a 2-dimensional array:

// Creating a 2D array
val array = Array(
  Array(1, 2, 3),
  Array(4, 5, 6),
  Array(7, 8, 9)
)
println(array.map(_.mkString(", ")).mkString("\n"))
/* Outputs:
1, 2, 3
4, 5, 6
7, 8, 9
*/

In this example, array is a 2-dimensional array, much like a 3-story "apartment building," where each floor represents an inner array.

Indexing in Multidimensional Arrays

All arrays in Scala are 0-based. Suppose we want to access an apartment on the second floor (index 1) and deliver a package to the first unit (index 0) of this structure. Here's how you can do it:

// Accessing an element
println(array(1)(0))  // Outputs: 4

Here, the number 1 in the first pair of parentheses refers to the second inner array, and 0 refers to the first element of that array.

Updating Multidimensional Arrays

Continuing with the apartment-building analogy, consider the task of replacing the locker code (the second apartment on the first floor) with a new one. Here's how this update can be done:

// Updating an element
array(0)(1) = "New Code"
println(array.map(_.mkString(", ")).mkString("\n"))
/* Outputs:
1, New Code, 3
4, 5, 6
7, 8, 9
*/

Common Built-in Methods

Scala provides methods that are useful for working with arrays:

  1. Array.length: Think of it as counting how many floors are there in our "apartment building." It tells us the number of rows in an array:

    // Finding the number of rows
    val numFloors = array.length
    println(numFloors) // Outputs: 3
  2. Appending Elements: Although fixed-size arrays don't allow direct appending, Scala offers mutable collections like ArrayBuffer for such operations. For this lesson, we'll focus on the concept of appending new data by creating a new array:

    // Adding a new row to our array
    val newArray = array :+ Array("Unit-1", "Unit-2", "Unit-3")
    println(newArray.map(_.mkString(", ")).mkString("\n"))
    /* Outputs:
    1, New Code, 3
    4, 5, 6
    7, 8, 9
    Unit-1, Unit-2, Unit-3
    */
  3. Removing Elements: Scala arrays are fixed in size, so elements cannot be removed directly. Instead, one might use constructs like ArrayBuffer or create a new array excluding the desired elements. Here's an example of creating a new array without a specific row:

    // Removing the second row from the array
    val filteredArray = array.zipWithIndex.filterNot(_._2 == 1).map(_._1)
    println(filteredArray.map(_.mkString(", ")).mkString("\n"))
    /* Outputs:
    1, New Code, 3
    7, 8, 9
    */

In this example, the second row (index 1) is removed by filtering it out and creating a new array.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal