Introduction
Creating and Populating an Array
With Sequence Methods
With new
A Handful of Methods
Using range
Using fill
Using toArray
Accessing Elements of an Array
Length of an Array
Introduction
Arrays in Scala are collections which come under the sequence class. They are a mutable collection, hence, when elements in an array are modified, the original array is updated. However, it is important to mention here that in one-way arrays are also immutable as the size of the array cannot change.
Scala arrays are represented as Java arrays. So, the Scala array Array[Int], is represented as the Java array int[] and the Scala array Array[String] is represented as the Java array string[], and so on. That being said, Scala arrays have a lot more to offer and in the next two lessons, we will see exactly how that is.
Creating and Populating an Array
While there are multiple ways of populating and defining an array in Scala, the basic syntax of creating an array from scratch remains the same with the keyword Array followed by () in which you mention the elements you want to populate your array with.
The Array method is creating an Array object.
Let’s look at a simple example where we want to create and populate an array with the first ten positive integers.