Arrays and Lists in Scala
In this lesson, you will learn about Arrays and Lists in Scala. These are two of the most commonly used collection types and are fundamental to everyday Scala programming.
Although Arrays and Lists may look similar, they differ significantly in mutability, performance, and use cases. Understanding these differences is critical for writing efficient and correct Scala code.
What Is an Array?
An Array is a mutable collection that stores elements in a fixed-size sequence. Once created, the size of an array cannot change, but its elements can be modified.
Arrays provide fast access to elements using index positions.
Creating an Array
val numbers = Array(10, 20, 30, 40)
println(numbers.toList)
Here, an array of integers is created.
The toList method is used only for display purposes.
Accessing Array Elements
Array elements are accessed using index positions. Scala uses zero-based indexing.
println(numbers(0))
println(numbers(2))
This retrieves the first and third elements of the array.
Modifying an Array
Because arrays are mutable, you can change their values.
numbers(1) = 99
println(numbers.toList)
This updates the element at index 1.
What Is a List?
A List is an immutable collection. Once a list is created, its elements cannot be changed.
Lists are optimized for functional programming and are widely used in Scala applications.
Creating a List
val fruits = List("Apple", "Banana", "Orange")
println(fruits)
This creates an immutable list of strings.
Accessing List Elements
List elements can also be accessed using index positions.
println(fruits(0))
println(fruits(2))
Although indexing is supported, lists are internally optimized for sequential access.
Adding Elements to a List
Since lists are immutable, adding elements creates a new list.
val newFruits = "Mango" :: fruits
println(newFruits)
The :: operator adds an element to the beginning of the list.
Array vs List: Key Differences
The table below summarizes the differences:
- Array → Mutable, fixed size, fast index access
- List → Immutable, flexible size, functional-friendly
Choosing the correct structure depends on your use case.
When to Use Arrays
Use arrays when:
- You need mutable data
- You require fast index-based updates
- The size of data is fixed
When to Use Lists
Use lists when:
- You prefer immutability
- You work with functional programming patterns
- You want safer, predictable code
Iterating Over Arrays and Lists
for (item <- fruits) {
println(item)
}
This loop works the same way for both arrays and lists.
📝 Practice Exercises
Exercise 1
Create an array of five numbers and modify one element.
Exercise 2
Create a list of three cities and add a new city to it.
Exercise 3
Print all elements of a list using a loop.
✅ Practice Answers
Answer 1
val arr = Array(1, 2, 3, 4, 5)
arr(2) = 99
println(arr.toList)
Answer 2
val cities = List("Paris", "London", "Tokyo")
val updated = "Berlin" :: cities
println(updated)
Answer 3
for (c <- cities) {
println(c)
}
What’s Next?
In the next lesson, you will learn about Sets and Maps, which are used to store unique values and key-value pairs.