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


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


在 Scala Stack class,則使用equals()方法檢查兩個堆棧是否由相同順序的相同元素組成。

函數定義: def equals(o: Any): Boolean

返回類型: It returns true if both the stacks are same or else returns false.



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



相關用法


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