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


Java Java.util.Arrays.deepEquals()用法及代碼示例



Arrays.deepEquals()用於檢查一維數組或多維數組的兩個數組是否相等。它可以比較兩個嵌套數組(即多維數組),而不管其尺寸如何。

  • 如果兩個數組引用都為null,或者兩個數組引用都引用了包含相同數量元素的數組,並且兩個數組中所有對應的元素對都非常相等,則認為它們是完全相等的。
  • 如果滿足以下任一條件,則兩個可能為null的元素e1和e2完全相等:
    • e1和e2都是對象引用類型的數組,並且Arrays.deepEquals(e1,e2)將返回true
    • e1和e2是相同原始類型的數組,並且Arrays.equals(e1,e2)的適當重載將返回true。
    • e1 == e2
    • e1.equals(e2)將返回true。

    請注意,此定義允許在任何深度使用null元素。

  • 它是數組類的一種方法

用法:


public static boolean deepEquals(Object[] o1, Object[] o2)

o1 = First Array to test for Equality
o2 = Second Array to test for Equality

Returns true if two array are equal
// Java program to demonstrate working of deepEquals. 
import java.util.Arrays; 
public class GFG { 
public static void main(String[] args) 
    { 
        int a1[][] = { { 10, 20 }, 
                       { 40, 50 }, 
                       { 60, 70 } }; 
  
        int a2[][] = { { 30, 20 }, 
                       { 10, 0 }, 
                       { 60, 80 } }; 
  
        int a3[][] = { { 10, 20 }, 
                       { 40, 50 }, 
                       { 60, 70 } }; 
        System.out.println("Check if a1 is equal to a2:"
                           + Arrays.deepEquals(a1, a2)); 
  
        System.out.println("Check if a2 is equal to a3:"
                           + Arrays.deepEquals(a2, a3)); 
  
        System.out.println("Check if a1 is equal to a3:"
                           + Arrays.deepEquals(a1, a3)); 
    } 
}

輸出:

Check if a1 is equal to a2:false
Check if a2 is equal to a3:false
Check if a1 is equal to a3:true

我們甚至可以使用deepEquals()來測試用戶定義類的Object數組的相等性。請參考下麵的例子
我們應該重寫equals方法,以在用戶定義的類中定義不同參數的相等性。

// Java program to demonstrate working of deepEquals 
// for arrays of user defined obj. 
import java.util.Arrays; 
public class GFG { 
public static class Employee { 
  
        int Eid; 
        String Ename; 
  
    public Employee(int Eid, String Ename) 
        { 
            this.Eid = Eid; 
            this.Ename = Ename; 
        } 
  
        // Overriding the equals() 
    public boolean equals(Object obj) 
        { 
  
            // type casting obj to Employee 
            Employee s = (Employee)obj; 
            return (this.Eid == s.Eid && this.Ename.equals(s.Ename)); 
        } 
    } public static void main(String args[]) 
    { 
        // Creating an array of objects of user defined class. 
        Employee e1[][] = { { new Employee(10, "Geek1"), 
                              new Employee(11, "Geek2") }, 
                            { new Employee(12, "Geek3"), 
                              new Employee(13, "Geek4") } }; 
  
        Employee e2[][] = { { new Employee(10, "Geek1"), 
                              new Employee(11, "Geek2") }, 
                            { new Employee(12, "Geek3"), 
                              new Employee(13, "Geek4") } }; 
  
        Employee e3[][] = { { new Employee(12, "Geek2"), 
                              new Employee(25, "Geek4") }, 
                            { new Employee(15, "Geek3"), 
                              new Employee(30, "Geek1") } }; 
  
        System.out.println("Check if e1 is equal to e2:"
                           + Arrays.deepEquals(e1, e2)); 
  
        System.out.println("Check if e2 is equal to e3:"
                           + Arrays.deepEquals(e2, e3)); 
  
        System.out.println("Check if a1 is equal to a3:"
                           + Arrays.deepEquals(e1, e3)); 
    } 
}

輸出:

Check if e1 is equal to e2:true
Check if e2 is equal to e3:false
Check if a1 is equal to a3:false

Equals() vs deepEquals()

雖然Arrays.equals()可在單維數組上正常工作,但它無法檢查多維數組的相等性。盡管Arrays.deepEquals()可以在所有數組上使用,無論其尺寸如何。

參考:
https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html#deepEquals-java.lang.Object:A-java.lang.Object:A-



相關用法


注:本文由純淨天空篩選整理自 Java.util.Arrays.deepEquals() in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。