Scala Stack last() method with example
Last Updated :
03 Nov, 2019
In Scala Stack class
, the last() method is utilized to return the last element of the stack.
Method Definition: def last: A
Return Type: It returns the last element of the stack.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 1 , 3 , 2 , 7 , 6 , 5 )
println(s 1 )
val result = s 1 .last
print( "The last element of the stack: " + result)
}
}
|
Output:
Stack(1, 3, 2, 7, 6, 5)
The last element of the stack: 5
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 1 , 3 , 2 , 7 , 6 , 13 )
println(s 1 )
val result = s 1 .last
print( "The last element of the stack: " + result)
}
}
|
Output:
Stack(1, 3, 2, 7, 6, 13)
The last element of the stack: 13
Share your thoughts in the comments
Please Login to comment...