Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
communitycreator

What is stack.size() in Scala?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Stack.size function in Scala returns the number of elements in a Stack object. In short, this function is used to get the current size of the Stack object.

The following illustration shows a visual representation of the Stack.size function.

A visual representation of the Stack.size function

To use the Stack.size function, you must include the following module:

scala.collection.mutable.Stack

Syntax

stack_name.size;
// where the stack_name is the name of the stack object

Parameter

The Stack.size function does not require any parameters.

Return value

This function returns the number of elements in the Stack object.

Code

The following code shows how to use the Stack.size function.

import scala.collection.mutable.Stack
object Main extends App {
var stack = Stack[Int]()
stack.push(0);
stack.push(2);
stack.push(5);
stack.push(3);
stack.push(1);
println("The following are the elements in the stack: " + stack);
println("The following is the size of the stack: " + stack.size);
}

Explanation

The push() method is first used to add values to the object stack. The size() method in line 1111 returns the number of elements in the object stack.

RELATED TAGS

scala
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring