Scala Stack pop()方法与实例

Scala Stack pop()方法与实例

Scala Stack类中, pop() 方法被用来移除并返回堆栈顶部的元素。

方法定义: def pop():A

返回类型。它移除并返回堆栈顶部的元素。

例子 #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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程