当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。