Different ways to create and update an Array in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.7, “Different ways to create and update an Array in Scala”


You want to create and optionally populate an Array.


There are many different ways to define and populate an Array. You can create an array with initial values, in which case Scala can determine the array type implicitly:

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> val fruits = Array("Apple", "Banana", "Orange")
fruits: Array[String] = Array(Apple, Banana, Orange)

If you don’t like the type Scala determines, you can assign it manually:

// scala makes this Array[Double]
scala> val x = Array(1, 2.0, 33D, 400L)
x: Array[Double] = Array(1.0, 2.0, 33.0, 400.0)

// manually override the type
scala> val x = Array[Number](1, 2.0, 33D, 400L)
x: Array[java.lang.Number] = Array(1, 2.0, 33.0, 400)

You can define an array with an initial size and type, and then populate it later:

// create an array with an initial size
val fruits = new Array[String](3)

// somewhere later in the code ...
fruits(0) = "Apple"
fruits(1) = "Banana"
fruits(2) = "Orange"

You can create a var reference to an array in a class, and then assign it later:

// this uses a null. don't do this in the real world
var fruits: Array[String] = _

// later ...
fruits = Array("apple", "banana")

The following examples show a handful of other ways to create and populate an Array:

scala> val x = Array.range(1, 10)
x: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

scala> val x = Array.range(0, 10, 2)
x: Array[Int] = Array(0, 2, 4, 6, 8)

scala> val x = Array.fill(3)("foo")
x: Array[String] = Array(foo, foo, foo)

scala> val x = Array.tabulate(5)(n => n * n)
x: Array[Int] = Array(0, 1, 4, 9, 16)

scala> val x = List(1, 2, 3).toArray
x: Array[Int] = Array(1, 2, 3)

scala> "Hello".toArray
res0: Array[Char] = Array(H, e, l, l, o)


The Scala Array is an interesting creature: It’s mutable in that its elements can be changed, but it’s immutable in that its size cannot be changed. The first link in the See Also section provides this information about the Array:

“Scala arrays correspond one-to-one to Java arrays. That is, a Scala array Array[Int] is represented as a Java int[], an Array[Double] is represented as a Java double[] and an Array[String] is represented as a Java String[].”

The Array is an indexed sequential collection, so accessing and changing values by their index position is straightforward and fast. Once you’ve created an Array, access its elements by enclosing the desired element number in parentheses:

scala> val a = Array(1, 2, 3)
a: Array[Int] = Array(1, 2, 3)

scala> a(0)
res0: Int = 1

Just as you access an array element by index, you update elements in a similar way:

scala> a(0) = 10

scala> a(1) = 20

scala> a(2) = 30

scala> a
res1: Array[Int] = Array(10, 20, 30)

See Also

  • A thorough discussion of Array, including background on its implementation
  • Recipe 10.4, “Understanding the Performance of Collections” discusses Array class performance