Related Articles

Related Articles

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

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

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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


Output:

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



My Personal Notes arrow_drop_up
Recommended Articles
Page :