Scala Tutorial - Learn How To Use Scala's Mutable ArrayBuffer
Overview
In this tutorial, we will learn how to use Scala's Mutable ArrayBuffer to perform common operations such as initialize an ArrayBuffer, access elements at specific index, add and remove elements and create an empty ArrayBuffer.
And, don't forget to review the Data Structures tutorial before delving into Scala's Immutable and Mutable collections.
More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Mutable ArrayBuffer will be discussed in Chapter 8 on Collection Functions.
What is an ArrayBuffer?
As per the Scala Documentation, an ArrayBuffer is a mutable data structure which allows you to access and modify elements at specific index.
Compared to the previous tutorial on Array, an ArrayBuffer is resizable while an Array is fixed in size.
Steps
1. How to initialize an ArrayBuffer with 3 elements
The code below shows how to initialize an ArrayBuffer with 3 elements.
import scala.collection.mutable.ArrayBuffer
println("Step 1: How to initialize an ArrayBuffer with 3 elements")
val arrayBuffer1: ArrayBuffer[String] = ArrayBuffer("Plain Donut","Strawberry Donut","Chocolate Donut")
println(s"Elements of arrayBuffer1 = $arrayBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 1: How to initialize an ArrayBuffer with 3 elements
Elements of arrayBuffer1 = ArrayBuffer(Plain Donut, Strawberry Donut, Chocolate Donut)
2. How to access elements of an ArrayBuffer at specific index
The code below shows how to access elements of an ArrayBuffer at specific index.
println("\nStep 2: How to access elements of an ArrayBuffer at specific index")
println(s"Element at index 0 = ${arrayBuffer1(0)}")
println(s"Element at index 1 = ${arrayBuffer1(1)}")
println(s"Element at index 2 = ${arrayBuffer1(2)}")
You should see the following output when you run your Scala application in IntelliJ:
Step 2: How to access elements of an ArrayBuffer at specific index
Element at index 0 = Plain Donut
Element at index 1 = Strawberry Donut
Element at index 2 = Chocolate Donut
3. How to add elements to an ArrayBuffer using +=
The code below shows how to add elements to an ArrayBuffer using +=.
println("\nStep 3: How to add elements to an ArrayBuffer using +=")
arrayBuffer1 += "Vanilla Donut"
println(s"Elements of arrayBuffer1 = $arrayBuffer1")
// NOTE: arrayBuffer1 is mutable and hence we were able to add a new element to it
You should see the following output when you run your Scala application in IntelliJ:
Step 3: How to add elements to an ArrayBuffer using +=
Elements of arrayBuffer1 = ArrayBuffer(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut)
4. How to add elements from a List to an ArrayBuffer using ++=
The code below shows how to add elements from a List to an ArrayBuffer using ++=.
println("\nStep 4: How to add elements from a List to an ArrayBuffer using ++=")
arrayBuffer1 ++= List[String]("Glazed Donut", "Krispy creme")
println(s"Elements of arrayBuffer1 = $arrayBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 4: How to add elements from a List to an ArrayBuffer using ++=
Elements of arrayBuffer1 = ArrayBuffer(Plain Donut, Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut, Krispy creme)
5. How to remove elements from an ArrayBuffer
The code below shows how to remove elements from an ArrayBuffer.
println("\nStep 5: How to remove elements from an ArrayBuffer")
arrayBuffer1 -= "Plain Donut"
println(s"Elements of arrayBuffer1 = $arrayBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 5: How to remove elements from an ArrayBuffer
Elements of arrayBuffer1 = ArrayBuffer(Strawberry Donut, Chocolate Donut, Vanilla Donut, Glazed Donut, Krispy creme)
6. How to remove elements of a List from ArrayBuffer using --=
The code below shows how to remove elements of a List from ArrayBuffer using --=.
println("\nStep 6: How to remove elements of a List from ArrayBuffer using --=")
arrayBuffer1 --= List[String]("Glazed Donut", "Krispy creme")
println(s"Elements of arrayBuffer1 = $arrayBuffer1")
You should see the following output when you run your Scala application in IntelliJ:
Step 6: How to remove elements of a List from ArrayBuffer using --=
Elements of arrayBuffer1 = ArrayBuffer(Strawberry Donut, Chocolate Donut, Vanilla Donut)
7. How to initialize an empty ArrayBuffer
The code below shows how to initialize an empty ArrayBuffer.
println("\nStep 7: How to initialize an empty ArrayBuffer")
val emptyArrayBuffer: ArrayBuffer[String] = ArrayBuffer.empty[String]
println(s"Empty array buffer = $emptyArrayBuffer")
You should see the following output when you run your Scala application in IntelliJ:
Step 7: How to initialize an empty ArrayBuffer
Empty array buffer = ArrayBuffer()
Summary
In this tutorial, we went over the following:
- How to initialize an ArrayBuffer with 3 elements
- How to access elements of an ArrayBuffer at specific index
- How to add elements to an ArrayBuffer using +=
- How to add elements from a List to an ArrayBuffer using ++=
- How to remove elements from an ArrayBuffer
- How to remove elements of a List from ArrayBuffer using --=
- How to initialize an empty ArrayBuffer
Tip
- Don't forget to review Chapter 8 on Collection Functions where we will go over the rich suite of functions exposed on collections such as aggregate, collect, fold, reduce, map, flatMap, scan, partition etc
- Scala's documentation on mutable ArrayBuffer.
Source Code
The source code is available on the allaboutscala GitHub repository.
What's Next
In the next tutorial, I will show you how to use Scala's Mutable ArrayStack.