Scala Stack splitAt() method with example
Last Updated :
03 Nov, 2019
In Scala Stack class
, the splitAt() method is utilized to split the given stack into a prefix/suffix pair of stacks at a stated position.
Method Definition: def splitAt(n: Int): (Stack[A], Stack[A])
Return Type: It returns a pair of stacks consisting of the first n elements of this stack, and the other elements.
Example #1:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 5 , 2 , 13 , 7 , 1 )
println(s 1 )
val result = s 1 .splitAt( 2 )
print(result)
}
}
|
Output:
Stack(5, 2, 13, 7, 1)
(Stack(5, 2), Stack(13, 7, 1))
Example #2:
import scala.collection.mutable. _
object GfG
{
def main(args : Array[String])
{
val s 1 = Stack( 5 , 2 , 13 , 7 , 1 )
println(s 1 )
val result = s 1 .splitAt( 3 )
print(result)
}
}
|
Output:
Stack(5, 2, 13, 7, 1)
(Stack(5, 2, 13), Stack(7, 1))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...