Related Articles

Related Articles

Scala Stack clear() method with example
  • Last Updated : 03 Nov, 2019

In Scala Stack class, the clear() method is utilized to delete all the elements of the stack.

Method Definition: def clear(): Unit

Return Type: It returns an empty queue.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of clear() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating stack  
        val s1 = Stack(1, 2, 3, 4, 5)  
            
        // Print the stack 
        println("Before clear() method: " + s1)  
          
        // Applying clear method  
        val result = s1.clear  
            
        // Displays output  
        print("After clear() method: " + result)
    

chevron_right


Output:

Before clear() method: Stack(1, 2, 3, 4, 5)
After clear() method: ()

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Scala program of clear() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
  
    // Main method 
    def main(args:Array[String]) 
    
      
        // Creating stack  
        val s1 = Stack("geeks", "for", "geeks")  
            
        // Print the stack 
        println("Before clear() method: " + s1)  
          
        // Applying clear method  
        val result = s1.clear  
            
        // Displays output  
        print("After clear() method: " + result)
    

chevron_right


Output:

Before clear() method: Stack(geeks, for, geeks)
After clear() method: ()



My Personal Notes arrow_drop_up
Recommended Articles
Page :