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 s1 = Stack(5, 2, 13, 7, 1) // Print the stack println(s1) // Applying splitAt method val result = s1.splitAt(2) // Display output print(result) } } |
chevron_right
filter_none
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 s1 = Stack(5, 2, 13, 7, 1) // Print the stack println(s1) // Applying splitAt method val result = s1.splitAt(3) // Display output print(result) } } |
chevron_right
filter_none
Output:
Stack(5, 2, 13, 7, 1) (Stack(5, 2, 13), Stack(7, 1))
Recommended Posts:
- Scala List splitAt() method with example
- Scala Set splitAt() method with example
- Scala Queue splitAt() method with example
- Scala SortedSet splitAt() method with example
- Scala TreeSet splitAt() method with example
- Scala Mutable SortedSet splitAt() method
- Scala immutable TreeSet splitAt() method
- Scala Stack ++ method with example
- Scala Stack ++:() method with example
- Scala Stack +:() method with example
- Scala Stack :+() method with example
- Scala Stack /:() method with example
- Scala Stack :() method with example
- Scala Stack push() method with example
- Scala Stack pop() method with example
- Scala Stack pushAll() method with example
- Scala Stack contains() method with example
- Scala Stack apply() method with example
- Scala Stack count() method with example
- Scala Stack copyToArray() method with example
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

