Open In App

Scala Stack splitAt() method with example

Last Updated : 03 Nov, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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) 
         
    


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) 
         
    


Output:

Stack(5, 2, 13, 7, 1)
(Stack(5, 2, 13), Stack(7, 1))


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads