Scala Tutorial - Learn How To Use Scala's Mutable ArrayStack

By Nadim Bahadoor | Last updated: July 25, 2017 at 14:44 pm

Overview

In this tutorial, we will learn how to use Scala's Mutable ArrayStack to perform common operations such as initialize an ArrayStack, access elements at specific index, pop and push elements and create an empty ArrayStack.

 

More advanced functions such as aggregate, fold, reduce, map, flatMap etc on the Mutable ArrayStack will be discussed in Chapter 8 on Collection Functions.

 

What is an ArrayStack?

As per Wikipedia, a Stack is a data structure which follows the LIFO (Last In First Out) semantics. It typically provides a push() method to add element at the top of the Stack and a pop() method to take the most recently added element from the top of the Stack.

 

As per Scala Documentation, an ArrayStack provides the Stack semantics while internally being backed by an Array data structure.

 

Steps

1. How to initialize ArrayStack with 3 elements

The code below shows how to initialize ArrayStack with 3 elements.


import scala.collection.mutable.ArrayStack
println("Step 1: How to initialize ArrayStack with 3 elements")
val arrayStack1: ArrayStack[String] = ArrayStack("Plain Donut", "Strawberry Donut", "Chocolate Donut")
println(s"Elements of arrayStack1 = $arrayStack1")

You should see the following output when you run your Scala application in IntelliJ:


Step 1: How to initialize ArrayStack with 3 elements
Elements of arrayStack1 = ArrayStack(Plain Donut, Strawberry Donut, Chocolate Donut)

 

2. How to check elements at specific index of an ArrayStack

The code below shows how to check elements at specific index of an ArrayStack.


println("\nStep 2: How to check elements at specific index of an ArrayStack")
println(s"Element at index 0 = ${arrayStack1(0)}")
println(s"Element at index 1 = ${arrayStack1(1)}")
println(s"Element at index 2 = ${arrayStack1(2)}")

You should see the following output when you run your Scala application in IntelliJ:

Step 2: How to check elements at specific index of an ArrayStack
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 ArrayStack using +=

The code below shows how to add elements to an ArrayStack using +=.


println("\nStep 3: How to add elements to an ArrayStack using +=")
arrayStack1 += "Vanilla Donut"
println(s"Elements of arrayStack1 = $arrayStack1")

You should see the following output when you run your Scala application in IntelliJ:


Step 3: How to add elements to an ArrayStack using +=
Elements of arrayStack1 = ArrayStack(Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)

 

4. How to add elements from a List to an ArrayStack using ++=

The code below shows how to add elements from a List to an ArrayStack using ++=.


println("\nStep 4: How to add elements from a List to an ArrayStack using ++=")
arrayStack1 ++= List[String]("Glazed Donut", "Krispy creme")
println(s"Elements of arrayStack1 = $arrayStack1")

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 ArrayStack using ++=
Elements of arrayStack1 = ArrayStack(Krispy creme, Glazed Donut, Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)

 

5. How to take an element from an ArrayStack using pop function

The code below shows how to take an element from an ArrayStack using pop function.


println("\nStep 5: How to take an element from an ArrayStack using pop function")
println(s"Pop element from stack = ${arrayStack1.pop}")
println(s"Elements of stack1 = $arrayStack1")

You should see the following output when you run your Scala application in IntelliJ:

Step 5: How to take an element from an ArrayStack using pop function
Pop element from stack = Krispy creme
Elements of stack1 = ArrayStack(Glazed Donut, Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)

 

6. How to push one element at the top of the ArrayStack using push function

The code below shows how to push one element at the top of the ArrayStack using push function.


println("\nStep 6: How to push one element at the top of the ArrayStack using push function")
arrayStack1.push("Krispy Creme")
println(s"Elements after push = $arrayStack1")

You should see the following output when you run your Scala application in IntelliJ:


Step 6: How to push one element at the top of the ArrayStack using push function
Elements after push = ArrayStack(Krispy Creme, Glazed Donut, Vanilla Donut, Plain Donut, Strawberry Donut, Chocolate Donut)

 

7. How to initialize an empty ArrayStack

The code below shows how to initialize an empty ArrayStack.


println("\nStep 7: How to initialize an empty ArrayStack")
val emptyArrayStack: ArrayStack[Nothing] = ArrayStack.empty
println(s"Empty Stack = $emptyArrayStack")

You should see the following output when you run your Scala application in IntelliJ:


Step 7: How to initialize an empty ArrayStack
Empty Stack = ArrayStack()

 

This concludes our tutorial on Learn How To Use Scala's Mutable ArrayStack and I hope you've found it useful!

 

Stay in touch via Facebook and Twitter for upcoming tutorials!

 

Don't forget to like and share this page :)

Summary

In this tutorial, we went over the following:

  • How to initialize an ArrayStack
  • How to check elements at specific index of an ArrayStack
  • How to add elements to an ArrayStack using +=
  • How to add elements from a List to an ArrayStack using ++=
  • How to take an element from an ArrayStack using pop function
  • How to push one element at the top of the ArrayStack using push function
  • How to initialize an empty ArrayStack

Tip

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 ListBuffer.

 

Nadim Bahadoor on FacebookNadim Bahadoor on GithubNadim Bahadoor on LinkedinNadim Bahadoor on Twitter
Nadim Bahadoor
Senior Software Developer | Elsevier
Founder of allaboutscala.com. I have over 10 years of experience in building large scale real-time trading systems in the financial industry. Passionate about Distributed Systems, Scala, Big Data and Functional Programming. Stay in touch for upcoming tutorials!
Other allaboutscala.com tutorials you may like:

Share this article on