📜  Scala Stack clear() 方法与示例

📅  最后修改于: 2022-05-13 01:55:26.965000             🧑  作者: Mango

Scala Stack clear() 方法与示例

在 Scala Stack class中, clear()方法用于删除堆栈的所有元素。

示例 #1:

// 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)
    } 
} 
输出:
Before clear() method: Stack(1, 2, 3, 4, 5)
After clear() method: ()

示例 #2:

// 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)
    } 
} 
输出:
Before clear() method: Stack(geeks, for, geeks)
After clear() method: ()