📜  Scala Stack equals() 方法与示例

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

Scala Stack equals() 方法与示例

在 Scala Stack class中, equals()方法用于检查两个堆栈是否由相同顺序的相同元素组成。

示例 #1:

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

示例 #2:

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

堆栈类,div>