Start learning free

LESSON 93 OF 414 · ARRAYS I

Static Arrays in Scala: Creating Fixed-Size Arrays

Lesson Read the full explanation

When we're dealing with large amounts of data, we want to make sure we can organize and manage it properly.

In Scala, we use arrays to store several pieces of data in one place. The items in an array must be of the same data type.

A static array or fixed array is an array for which the size / length is determined when the array is created and/or allocated.


Creation:

We create a static array with the new keyword, the element type inside Array[], and the length inside parentheses:

val arrayOfStrings = new Array[String](3)

In this program we created an array named arrayOfStrings that consists of 3 String items.

These 3 items are null (no value given yet) initially.


Creation & Initialization:

There is a way to initialize an array at creation by using the Array(...) factory, listing the values inside the parentheses and putting a comma between each entry, like this:

val arrayOfStrings = Array("AlgoCademy", "coding", "static arrays")

We created an array named arrayOfStrings which consists of 3 String items: "AlgoCademy", "coding" and "static arrays".


Assignment
Follow the Coding Tutorial and let's play with some arrays.


Hint
Look at the examples above if you get stuck.


STEP 1 OF 2
    Ctrl + Enter to check

    SCALA · ARRAYS I

    Static Arrays in Scala, explained

    What a static array is

    An array stores several values of the same type in one variable, in order. A static (or fixed) array has its length decided when it is created, and that length never changes. In Scala this is the Array[T] class, where T is the element type: Array[Int] holds integers and Array[String] holds strings.

    Use an array when you know how many items you need up front and want fast access by position. When the number of items has to grow or shrink, a collection such as ArrayBuffer fits better.

    Two ways to create one

    With a length: new Array[T](n)

    This allocates an array of n slots and fills them with the default value for the type:

    val arrayOfStrings = new Array[String](3)
    val numbers = new Array[Int](20)
    

    The element type goes in square brackets and the length in parentheses. The slots are not empty: a String array starts with three null values, an Int array with zeros and a Boolean array with false. Printing arrayOfStrings.mkString(",") gives null,null,null, and numbers.sum is 0.

    With values: Array(...)

    To fill the array at creation, list the values inside Array(...), separated by commas:

    val arrayOfStrings = Array("AlgoCademy", "coding", "static arrays")
    

    There is no new here, because Array(...) calls the companion object's apply method. Scala infers the element type from the values, so this is an Array[String] of length 3. All values must fit one type. Array(1, 2.5) becomes an Array[Double], because the integer is widened to a double.

    Using a fixed array

    After creation, you can read and replace elements by index, and ask for the length. The length is fixed, but the contents are not:

    val nums = new Array[Int](3)
    nums(1) = 7
    println(nums.mkString("[", ", ", "]")) // [0, 7, 0]
    println(nums.length)                   // 3
    

    Only slot 1 changed. The other two keep the default 0. Writing to an index that doesn't exist, such as nums(3) = 1, throws an ArrayIndexOutOfBoundsException, because the array has no fourth slot to hold it.

    The exercise

    The lesson asks for two arrays:

    1. An integer array named numbers with 20 elements. Use the length form, since no values are given: the element type is Int and the length is 20.
    2. A string array named fruits holding "mango", "kiwi" and "grapefruit". Use the value form, since the contents are known.
    val numbers = new Array[Int](20)
    val fruits = Array("mango", "kiwi", "grapefruit")
    

    Gotchas

    • val fixes the variable, not the contents. A val array always refers to the same array, but its elements can still be changed. You can't give numbers a different array later.
    • Array(3) is not new Array[Int](3). Array(3) is an array with one element, the number 3. new Array[Int](3) is an array of three zeros.
    • Don't print an array directly. println(fruits) shows a type-and-address string, not the contents. Use fruits.mkString(", ") or fruits.toList.
    • The length can't change. There is no way to add a 21st element to numbers. You would create a new array or use an ArrayBuffer.

    Why a default value matters

    The defaults make a freshly created array safe to read. A new Array[Int] works as a set of counters that all start at zero. A new Array[String] is different: reading a slot before you assign it gives null, and calling a method on it, such as .length, fails with a NullPointerException. Assign every slot before you use it, or build the array from values with Array(...).

    Key takeaways

    • new Array[T](n) makes n default-valued slots: zeros, false or null.
    • Array(a, b, c) makes an array from values and infers the type.
    • An array's length is fixed when it is created.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free