Small steps to Scala (3/20) - arrays

Scala
3/20

Arrays

  • fixed length arrays
  • variable length arrays
  • traversing arrays
  • transforming arrays
  • common methods
  • multidimensional arrays

Fixed length arrays

This is an example on how to define a fixed length array

val numbers = new Array[Int](5) // array of Int with a fix size of 5
val greeting = Array("Hello", "Hi") // array of String with the fix size of 2

To modify the contents of a fixed size array, you can re-assign a new value of the same type to the index that has to be modified. For example:

> greeting(1) = "こんにちは"
res1: Array[String] = Array(Hello, こんにちは)

Arrays in Scala (like in most of the programming languages) are 0 indexed. Calling greeting(2) would result in an java.lang.ArrayIndexOutOfBoundsException error.
Notice the notation round brackets () instead of squared [] for accessing an array index.

Variable length arrays

Variable length arrays are also called buffers and in order to construct one, you need to import first scala.collection.mutable.ArrayBuffer

import scala.collection.mutable.ArrayBuffer

val b = ArrayBuffer[Int]()  // empty array
b += 1
b += (2, 3, 4, 5)
b ++= Array(3, 4, 5, 6) // to append
b.trimEnd(5) // remove last 5 elements of the array

res18: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3, 4)

Inserting an element before index 2.

> b.insert(2, 15)
res20: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 15, 3, 4)

Remove 2 elements starting from index 1

> b.remove(1, 2)
res22: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 3, 4)

Traversing arrays

Traversing an array can be done with a very simple one line for loop

for (i <- b) println(i)

Transforming arrays

Assuming we have the following array

val a = Array(1, 2, 3, 4, 5, 6)

and we want to multiply each element with 2,

val mult = for (i <- a) yield 2 * i

the result would be

mult: Array[Int] = Array(2, 4, 6, 8, 10, 12)

In order to traverse an array and add some conditions, we would do something like:

> for (i <- a if i % 2 == 0) yield 2 * i
res25: Array[Int] = Array(4, 8, 12)

Same as above, with the same results, but in a functional way

a.filter(x => x % 2 == 0).map(x => 2 * x)

Now, in a more _Scala_esque way, with the same results:

a.filter(_ % 2 == 0).map(2 * _)

In Scala there are several uses of the _ underscore notation (see this link). In this case it servers as a placeholder syntax.

Common methods

Summation

> var a = Array(1, 5, 2, 3, 9, 4, 7, 11)
a: Array[Int] = Array(1, 5, 2, 3, 9, 4, 7, 11)
> a.sum
res27: Int = 42 

Sorting arrays

> val b = ArrayBuffer(1, 9, 3, 8, 2)
b: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 9, 3, 8, 2)
> b.sortWith(_ > _)
res28: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(9, 8, 3, 2, 1)

Sorting arrays in place

> scala.util.Sorting.quickSort(a) // sorting in place
> a
res30: Array[Int] = Array(1, 2, 3, 4, 5, 7, 9, 11)

NOTE that arrayBuffers cannot be sorted in place. Trying to do that it would result in a Compile Error

Here are some other methods for the Array type and for the ArrayBuffer type.

Multidimensional arrays

This is an example of a 2 rows by 3 columns empty array

val matrix = Array.ofDim[Double](2, 3)

To insert an element in a multidimensional array, all you have to do is to re-assign a value to the proper index coordinates of the array.

> matrix(1)(2) = 22
> matrix
res38: Array[Array[Double]] = Array(Array(0.0, 0.0, 0.0), Array(0.0, 0.0, 22.0))

[+] Scala series
  • Small steps to Scala 1/20 - the basics
  • Small steps to Scala 2/20 - functions

  • Disclaimer: This is by no means an original work it is merely meant to serve as a compilation of thoughts, code snippets and teachings from different sources.