Scala Stack drop() method with example
In Scala Stack class
, the drop() method is utilized to drop the first ‘n’ elements from the top of the stack.
Method Definition: def drop(n: Int): Stack[A]
Return Type: It returns a new stack with all the elements except the first ‘n’ ones from the top of the given stack.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 6 , 2 , 3 , 4 , 5 )
println(s 1 )
val result = s 1 .drop( 2 )
print( "Stack after drop(2) method: " + result)
}
}
|
Output:
Stack(6, 2, 3, 4, 5)
Stack after drop(2) method: Stack(3, 4, 5)
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 6 , 2 , 3 , 4 , 5 )
println(s 1 )
val result = s 1 .drop( 3 )
print( "Stack after drop(3) method: " + result)
}
}
|
Output:
Stack(6, 2, 3, 4, 5)
Stack after drop(3) method: Stack(4, 5)
Last Updated :
03 Nov, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...