📜  Scala Stack pushAll() 方法与示例

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

Scala Stack pushAll() 方法与示例

在 Scala Stack class中, pushAll()方法用于将集合中的元素添加到堆栈中。

示例 #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++)