Start learning free

LESSON 90 OF 348 · ARRAYS I

Accessing static array elements in Scala in Scala

Lesson

Arrays are ordered, meaning each element has a numbered position known as its index. We access an array element by referring to its index number.

Arrays use zero-based indexing, so the first element in an array has an index of 0, the second element has an index of 1, etc.

We use the index in parentheses (()) after the name of the array to access the element:

val animals = Array("cat", "dog", "parrot")

println(animals(0)) // Output: "cat"
println(animals(1)) // Output: "dog"
println(animals(2)) // Output: "parrot"

Assignment
Follow the Coding Tutorial and let's practice with accessing elements!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 3
    Ctrl + Enter to check

    Accessing Static Array Elements in Scala

    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free