📜  Scala Stack reverse() 方法与示例

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

Scala Stack reverse() 方法与示例

在 Scala Stack class中,使用reverse()方法以相反的顺序返回堆栈。

示例 #1:

// Scala program of reverse() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating stack
        val s1 = Stack(1, 2, 3, 4, 5, 6) 
          
        // Print the stack 
        println(s1) 
           
        // Applying reverse method  
        val result = s1.reverse
          
        // Display output 
        print("Reversed stack: " + result) 
           
    } 
} 
输出:
Stack(1, 2, 3, 4, 5, 6)
Reversed stack: Stack(6, 5, 4, 3, 2, 1)

示例 #2:

// Scala program of reverse() 
// method 
  
// Import Stack 
import scala.collection.mutable._
  
// Creating object 
object GfG 
{ 
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Creating stack
        val s1 = Stack(5, 2, 13, 7, 1, 3) 
          
        // Print the stack 
        println(s1) 
           
        // Applying reverse method  
        val result = s1.reverse
          
        // Display output 
        print("Reversed stack: " + result) 
           
    } 
} 
输出:
Stack(5, 2, 13, 7, 1, 3)
Reversed stack: Stack(3, 1, 7, 13, 2, 5)