Scala Stack splitAt() method with example
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:
// Scala program of splitAt() // method // Import Stack import scala.collection.mutable. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating stack val s 1 = Stack( 5 , 2 , 13 , 7 , 1 ) // Print the stack println(s 1 ) // Applying splitAt method val result = s 1 .splitAt( 2 ) // Display output print(result) } } |
Output:
Stack(5, 2, 13, 7, 1) (Stack(5, 2), Stack(13, 7, 1))
Example #2:
// Scala program of splitAt() // method // Import Stack import scala.collection.mutable. _ // Creating object object GfG { // Main method def main(args : Array[String]) { // Creating stack val s 1 = Stack( 5 , 2 , 13 , 7 , 1 ) // Print the stack println(s 1 ) // Applying splitAt method val result = s 1 .splitAt( 3 ) // Display output print(result) } } |
Output:
Stack(5, 2, 13, 7, 1) (Stack(5, 2, 13), Stack(7, 1))