Scala Stack push() method with example
Last Updated :
03 Nov, 2019
In Scala Stack class
, the push() method is utilized to add an element on the top of the stack.
Method Definition: def push(elem: A): Stack.this.type
Return Type: It adds an element on the top of the stack.
Example #1:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
var s = Stack[Int]()
s.push( 1 )
s.push( 2 )
s.push( 3 )
s.push( 4 )
println(s)
}
}
|
Output:
Stack(4, 3, 2, 1)
Example #2:
import scala.collection.mutable.Stack
object GfG
{
def main(args : Array[String])
{
var s = Stack[String]()
s.push( "C++" )
s.push( "Java" )
s.push( "Python" )
s.push( "Scala" )
println(s)
}
}
|
Output:
Stack(Scala, Python, Java, C++)
Share your thoughts in the comments
Please Login to comment...