当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Scala Stack pop()用法及代码示例


在 Scala Stack class,则使用pop()方法删除并返回堆栈顶部的元素。

函数定义: def pop(): A

返回类型: It removes and returns the element at the top of the stack.



范例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


相关用法


注:本文由纯净天空筛选整理自rupesh_rao大神的英文原创作品 Scala Stack pop() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。