Course Content
Introduction to Scala
Introduction to Scala
What is an Array?
Before we proceed to working with arrays in Scala, let's first discuss what an array actually is.
Declaring an Array Variable
In order to work with arrays in Scala, you must first declare a variable which references the array with the specified type of array.
Let's now take a look at the general syntax for declaring an array variable in Scala, where arrayName
is simply the name of the variable:
Main
// The first option var arrayName: Array[dataType] = new Array[dataType](size) // The second option (type inference) var arrayName = new Array[dataType](size)
The second option is the preferred one since the exact array data type is specified on the right side of the assignment expression anyway. Moreover, the data type of the array elements (dataType
) can be any of the basic data types and even custom classes.
It is also mandatory to specify the size of the array (size
) within the parentheses ()
.
Note that a new
keyword is used here. Here's what it signifies:
- Object Creation: It tells the program to allocate memory for a new object.
- Constructor Invocation: It calls the constructor of the class to initialize the new object. Constructors can be thought of as special methods in a class that are called when an object is instantiated.
- Returning a Reference: After creating the new object,
new
returns a reference to it.
Classes are out-of-scope for this course, however, here's a brief explanation of classes if you're interested:
Creating Your First Array
Let's now create our first array in Scala of type Int
and size equal to 5
:
Main
object Main { def main(args: Array[String]): Unit = { var intArray = new Array[Int](5) println(intArray.length) } }
First, we created our intArray
variable and initialize it with as an Array
of Int
elements of size 5.
Afterward, we also printed the length (size) of the array using the intArray.length
syntax. This accesses the length property of the intArray
array. Properties are called using the dot (.
) notation on the object with them and store certain info about the object.
Accessing Array Elements
Arrays are also indexed, with the indexing starting at 0 (such indexing is called zero-indexing) which means that we can access its element via specifying the index of this element.
Let's take a look at the following illustration to make things clear:
As you can see, the indices go from 0 to length - 1, where length is the length of the array.
In order to access an array element by its index, the following syntax is used:
Main
arrayName(index)
arrayName
is just the name for the array variable, and index
is just the index of the element. The index is simply an integer number. In Scala, we should always enclose the index of the element in parentheses ()
.
Now, it's time to access array elements by indices in code:
Main
object Main { def main(args: Array[String]): Unit = { var intArray = new Array[Int](5) intArray(0) = 10 intArray(1) = 8 intArray(2) = 210 intArray(3) = 5 intArray(4) = 20000 println(intArray(4)) } }
We have populated our array with five integers via accessing each element by its index and assigning a certain integer number to it. Moreover, we printed the fifth element of our array which has index 4
.
Let's illustrate this with an example:
Main
object Main { def main(args: Array[String]): Unit = { var intArray = new Array[Int](5) intArray(5) = 2 // ArrayIndexOutOfBoundsException will be thrown } }
Declaring an Array with Predefined Elements
While the approach above to fill an array works just fine, it requires a lot of lines of code, especially for a relatively large array. Let's take a look at a more concise approach when we declare an array with predefined elements:
Main
object Main { def main(args: Array[String]): Unit = { var intArray = Array(10, 8, 210, 5, 20000) println(intArray(4)) } }
The elements of the array in this case should be enclosed within the parentheses ()
and separated by commas.
Moreover, there is no need to specify the array size explicitly since the compiler determines the size of the array based on the provided elements.
Thanks for your feedback!