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


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


在 Scala Stack class,則使用diff()方法來查找兩個堆棧之間的差異。它從另一個堆棧中刪除一個堆棧中存在的元素。

函數定義: def diff[B >: A](that: collection.Seq[B]): Stack[A]

返回類型: It returns a new stack which consists of elements after the difference between the two stacks.



範例1:

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

範例2:

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


相關用法


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