1. Arrays
Great work! Next, you'll learn to structure your data in more complex ways using collections. The first collection you'll learn: Array.
2. Collections
Collections are incredibly useful and you'll use them a lot. Like variables, there are two kinds: mutable and immutable.
Mutable collections can be updated or extended in place. That is, you can change, add, or remove elements of the originally created collection.
Immutable collections never change. Operations exist that simulate additions, removals, or updates, but those operations return a new collection and leave the old collection unchanged.
3. Array
Array is a mutable sequence of objects that all share the same type.
In the Scala interpreter, here's an array for our players, Alex, Chen and Marta. val players equals capital A Array, then player names in quotations separated by commas. Here, the creation, parameterization, and initialization is done all at once, with Scala doing some work behind the scenes for us. Pause and take a second to read the definitions of parameterization and initialization. If we want to be more explicit,
4. Array
we separate the parameterization from the initialization. Let's look at parameterizing type first. val players new Array of type string (which goes in square brackets). All collections have a type parameter specifying what they hold, in this case, String, which can be inferred with type inference from the initialization content, like Alex, Chen, Marta in quotes from last slide. Next, parameter values. We give the length parameter a value of 3 here, which goes in parentheses. To be even more explicit
5. Array
we write the type parameterization after a colon and before the equals sign. The type parameterization is part of the instance, while the value parameterization (3 for length) is not. That is, the type of players is Array String, not Array String 3.
6. Array
Initializing the array is done by passing in data inside parentheses, not inside square brackets like in Java and Python. Arrays being sequences means that they have order. Collections are zero-indexed in Scala, so players zero is the first element, which is the String Alex. Hey, Alex!
7. Arrays are mutable
Note that the array players is a val, and vals cannot be reassigned. However, if the object to which that val refers is mutable, that object CAN change! Say if Alex leaves the game (bye Alex!) and Sindhu comes in to fill their place, we can update that data. players zero equals Sindhu. Boom, a val's elements updated in place, which is possible since Arrays are mutable.
8. Arrays are mutable
That said, you can only update arrays with values of the same type of the array. For example, if we update players index 0 with the Int 500, an error of type mismatch is thrown.
9. Recommendation: use val with Array
In Scala, it is recommended that you make mutable collections vals so you don't have to keep track of two things that can change. If we made players a var, the elements in our original array could change, like the update to include Sindhu, AND an entirely different Array object could be assigned to players.
10. Scala nudges us towards immutability
As you know, Scala nudges us towards immutability, and arrays are NOT immutable. Because collections store more data than the single value variables you previously created, immutability with collections increases the potential for hard-to-find bugs.
11. The Any supertype
Another tidbit: you can mix and match types by parameterizing with Any, the supertype of all types. You'll learn more about Scala's type hierarchy in more advanced courses.
12. Let's practice!
You structured data with Array. Get practicing what you just learned while it's still fresh.