Scala – Arrays

  • Post category:Scala
  • Reading time:4 mins read

Scala provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.

Basic Array Operations

Declaring an Array

var arrayname = new Array[datatype](size)
Example: var arr = new Array[Int](3)

Here, arr is declared as an array of type Int that may hold up to three elements.

Inserting Elements

arr(0) = 100
arr(1) = 200
arr(2) = 300

Creating Array with Values (Initial elements)

var arrString = Array("Jan", "Feb", "Mar", "Apr") // Array of Strings.
val numbers = Array(1, 2, 3) // Array Of Ints.

// Array containing mixed types.
val x: Array[Double] = Array(1, 1.0, 1F) 
val x: Array[Number] = Array(1, 1.0, 1F)

val nums = Array[Int]() // Empty Array

Complete Example

object ArrayTest extends App {
   var numbers = new Array[](5)

   // Inserting elements in Array.
   numbers(0) = 100
   numbers(1) = 200
   numbers(2) = 300
   numbers(3) = 400
   numbers(4) = 500

   // Basic FOR loop over Arrays.
   for(x <- 0 until numbers.length){
      println(numbers(x))
   }

   // Itreate Over Arrays.
   for (x <- numbers) {
      println(x)
   }

   // Updating elements in Array.
   numbers(0) = 900
   numbers(1) = 800

   // Accessing elements in Array.
   println("Element at 0th index: "+numbers(0))
   println("Element at 1st index: "+numbers(1))
}

Output

100
200
300
400
500
100
200
300
400
500
Element at 0th index: 900
Element at 1st index: 800

Multi-Dimensional Arrays

Multi- Dimensional array can be created using Array.ofDim

Syntax

val rows = 2
val cols = 3
val a = Array.ofDim[String](rows, cols)
Output in REPL:
a: Array[Array[String]] = Array(Array(null, null, null), Array(null, null, null))

val x, y, z = 3
val a = Array.ofDim[Int](x, y, z)
Output in REPL:
a: Array[Array[Array[Int]]] = Array(Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)), Array(Array(0, 0, 0), Array(0, 0, 0), Array(0, 0, 0)))

Example – Two Dimensional Array

object MDArray extends App {
  val rows = 2
  val cols = 3
  val a = Array.ofDim[String](rows, cols)

  // Inserting elements in Array
  a(0)(0) = "Jan"
  a(0)(1) = "Feb"
  a(0)(2) = "Mar"
  a(1)(0) = "Apr"
  a(1)(1) = "May"
  a(1)(2) = "Jun"

  // Accessing the elements
  println("First element Row 1: "+a(0)(0))
  println("Second element Row 1: "+a(0)(1))
  println("Third element Row 1: "+a(0)(2))

  // Iterating the array
  for {
    i <- 0 until rows
    j <- 0 until cols
  } println(s"($i)($j) = ${a(i)(j)}")
}

Output

First element Row 1: Jan
Second element Row 1: Feb
Third element Row 1: Mar
(0)(0) = Jan
(0)(1) = Feb
(0)(2) = Mar
(1)(0) = Apr
(1)(1) = May
(1)(2) = Jun

Example – Three Dimensional Array

object ThreeDArray extends App {
   val x, y, z = 3
   val a = Array.ofDim[Int](x, y, z)

   // Inserting elements in Array
   a(0)(0)(0) = 100
   a(0)(0)(1) = 200
   a(0)(0)(2) = 300
   a(0)(1)(0) = 400
   a(0)(1)(1) = 500
   a(0)(1)(2) = 600

   // Iterating the array
   for {
      i <- 0 until x
      j <- 0 until y
      k <- 0 until z
   } println(s"($i)($j)($k) = ${a(i)(j)(k)}")
}

Output

(0)(0)(0) = 100
(0)(0)(1) = 200
(0)(0)(2) = 300