📜  Scala Stack pop() 方法与示例

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

Scala Stack pop() 方法与示例

在 Scala Stack class中, pop()方法用于移除并返回栈顶元素。

示例 #1:

// Scala program of pop() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
{ 
      
    // Main method 
    def main(args:Array[String]) 
    { 
          
        // Creating a stack
        var s = Stack("C++", "Java", "Python", "Scala") 
          
        // Print the stack
        println(s)
          
        // Print the top of the stack
        println("Top of the stack: " + s.top)
  
        // Applying pop method    
        val result = s.pop
          
        // Print the popped element
        println("Popped element: " + result) 
  
    } 
} 
输出:
Stack(C++, Java, Python, Scala)
Top of the stack: C++
Popped element: C++

示例 #2:

// Scala program of pop() 
// method 
  
import scala.collection.mutable.Stack 
  
// Creating object 
object GfG 
{ 
      
    // Main method 
    def main(args:Array[String]) 
    { 
          
        // Creating a stack
        var s = Stack(1, 2, 3, 4) 
          
        // Print the stack
        println(s)
          
        // Print the top of the stack
        println("Top of the stack: " + s.top)
  
        // Applying pop method    
        val result = s.pop
          
        // Print the popped element
        println("Popped element: " + result) 
  
    } 
} 
输出:
Stack(1, 2, 3, 4)
Top of the stack: 1
Popped element: 1