📜  Scala Stack diff() 方法与示例

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

Scala Stack diff() 方法与示例

在 Scala Stack class中, diff()方法用于查找两个堆栈之间的差异。它从另一个堆栈中删除存在于一个堆栈中的元素。

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