📜  Java中的Java .util.Arrays.deepEquals()

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

Java中的Java .util.Arrays.deepEquals()


Arrays.deepEquals() 用于检查单维或多维数组的两个数组是否相等。它可以比较两个嵌套数组(即多维数组),而不管其维度如何。

  • 如果两个数组引用都为空,或者它们引用的数组包含相同数量的元素并且两个数组中所有对应的元素对都非常相等,则认为两个数组引用是深度相等的。
  • 如果满足以下任一条件,则两个可能为 null 的元素 e1 和 e2 完全相等:
    • e1 和 e2 都是对象引用类型的数组,并且 Arrays.deepEquals(e1, e2) 将返回 true
    • e1 和 e2 是相同原始类型的数组,并且 Arrays.equals(e1, e2) 的适当重载将返回 true。
    • e1 == e2
    • e1.equals(e2) 将返回 true。

    请注意,此定义允许任何深度的空元素。

  • 它是 Arrays 类的方法

句法:

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() 与 deepEquals()

虽然 Arrays.equals() 在单维数组上正常工作,但它不能检查多维数组的相等性。
虽然 Arrays.deepEquals() 对所有数组起作用,但与维度无关。

参考 :
https://docs.oracle.com/javase/8/docs/api/java Java