當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Scala Stack pushAll()用法及代碼示例

在 Scala Stack class,則使用pushAll()方法將集合中的元素添加到堆棧中。

函數定義: def pushAll(elems: IterableOnce[A]): Stack.this.type

返回類型: It returns a stack that contains all the elements of the given collection.



範例1:

// Scala program of pushAll()  
// method  
  
import scala.collection.mutable.Stack  
  
// Creating object  
object GfG  
{  
      
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a stack 
        var s1 = Stack[String]() 
          
        // Creating a set 
        var s2 = Set("C++", "Java", "Python", "Scala")  
          
        // Print the set 
        println(s2) 
      
        // Applying pushAll method     
        s1.pushAll(s2) 
          
        // Print the stack 
        println(s1)  
  
    }  
} 
輸出:
Set(C++, Java, Python, Scala)
Stack(Scala, Python, Java, C++)

範例2:

// Scala program of pushAll()  
// method  
  
import scala.collection.mutable.Stack  
  
// Creating object  
object GfG  
{  
      
    // Main method  
    def main(args:Array[String])  
    {  
        // Creating a stack 
        var s1 = Stack[String]() 
          
        // Creating another stack 
        var s2 = Stack("C++", "Java", "Python", "Scala")  
          
        // Print the stack 
        println(s2) 
      
        // Applying pushAll method     
        s1.pushAll(s2) 
          
        // Print the stack 
        println(s1)  
  
    }  
} 
輸出:
Stack(C++, Java, Python, Scala)
Stack(Scala, Python, Java, C++)


相關用法


注:本文由純淨天空篩選整理自rupesh_rao大神的英文原創作品 Scala Stack pushAll() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。