Java Arrays deepEquals() 方法



描述

Java Arrays deepEquals(Object[] a1, Object[] a2) 方法返回 true,如果兩個指定的陣列彼此深度相等。如果兩個陣列引用都為 null,或者它們指向包含相同數量的元素並且兩個陣列中所有對應的元素對都深度相等,則認為它們深度相等。

兩個可能為 null 的元素 e1 和 e2 深度相等,如果滿足以下任何條件:

  • e1 和 e2 都是物件引用型別的陣列,並且 Arrays.deepEquals(e1, e2) 將返回 true。

  • e1 和 e2 是相同基本型別的陣列,並且 Arrays.equals(e1,e2) 的適當過載將返回 true。

  • e1 == e2

  • e1.equals(e2) 將返回 true。

宣告

以下是 java.util.Arrays.deepEquals() 方法的宣告

public static boolean deepEquals(Object[] a1, Object[] a2)

引數

  • a1 − 這是要測試相等的陣列。

  • a2 − 這是要測試相等的另一個數組。

返回值

如果兩個陣列相等,則此方法返回 true,否則返回 false。

異常

檢查字元陣列的深度相等示例

以下示例演示了 Java Arrays deepEquals(Object[], Object[]) 方法的使用。在此示例中,我們使用字元建立了三個物件陣列,並使用 deepEquals() 方法比較它們。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
   
      // initializing Object array1
      Object[] b1 = new Object[] { 'a', 'b' };

      // let us print all the values available in array2
      System.out.println("Elements of Array1 is:");
      for (Object value : b1) {
         System.out.println("Value = " + value);
      }

      // initializing Object array2
      Object[] b2 = new Object[] { 'a', 'b' };

      // let us print all the values available in array2
      System.out.println("Elements of Array2 is:");
      for (Object value : b2) {
         System.out.println("Value = " + value);
      }

      // initializing Object array3
      Object[] b3 = new Object[] { 'x', 'y' };

      // let us print all the values available in array3
      System.out.println("Elements of Array3 is:");
      for (Object value : b3) {
         System.out.println("Value = " + value);
      }

      // checking array1 and array2 for equality
      System.out.println("Array1 and Array2 are equal:" + Arrays.deepEquals(b1,b2));

      // checking array1 and array3 for equality
      System.out.println("Array1 and Array3 are equal:" + Arrays.deepEquals(b1,b3));
   }
}  

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Elements of Array1 is:
Value = a
Value = b
Elements of Array2 is:
Value = a
Value = b
Elements of Array3 is:
Value = x
Value = y
Array1 and Array2 are equal:true
Array1 and Array3 are equal:false

檢查物件的陣列的深度相等示例

以下示例演示了 Java Arrays deepEquals(Object[], Object[]) 方法的使用。在此示例中,我們使用 Student 物件建立了三個物件陣列,並使用 deepEquals() 方法比較它們。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // initializing Object array1
      Student[] b1 = new Student[] { new Student(1, "Julie"), new Student(2, "Robert") };

      // let us print all the values available in array2
      System.out.println("Elements of Array1 is:");
      for (Object value : b1) {
         System.out.println("Value = " + value);
      }

      // initializing Object array2
      Student[] b2 = new Student[] { new Student(1, "Julie"), new Student(2, "Robert") };

      // let us print all the values available in array2
      System.out.println("Elements of Array2 is:");
      for (Object value : b2) {
         System.out.println("Value = " + value);
      }

      // initializing Object array3
      Student[] b3 = new Student[] { new Student(1, "Julie"), new Student(3, "Adam") };

      // let us print all the values available in array3
      System.out.println("Elements of Array3 is:");
      for (Object value : b3) {
         System.out.println("Value = " + value);
      }

      // checking array1 and array2 for equality
      System.out.println("Array1 and Array2 are equal:" + Arrays.deepEquals(b1,b2));

      // checking array1 and array3 for equality
      System.out.println("Array1 and Array3 are equal:" + Arrays.deepEquals(b1,b3));
   }
} 
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }

   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Elements of Array1 is:
Value = [ 1, Julie ]
Value = [ 2, Robert ]
Elements of Array2 is:
Value = [ 1, Julie ]
Value = [ 2, Robert ]
Elements of Array3 is:
Value = [ 1, Julie ]
Value = [ 3, Adam ]
Array1 and Array2 are equal:true
Array1 and Array3 are equal:false

檢查整數陣列的深度相等示例

以下示例演示了 Java Arrays deepEquals(Object[], Object[]) 方法的使用。在此示例中,我們建立了三個 Integer 物件陣列,並使用 deepEquals() 方法比較它們。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {
   
      // initializing Object array1
      Integer[] b1 = new Integer[] { 1, 2 };

      // let us print all the values available in array2
      System.out.println("Elements of Array1 is:");
      for (Object value : b1) {
         System.out.println("Value = " + value);
      }

      // initializing Object array2
      Integer[] b2 = new Integer[] { 1, 2 };

      // let us print all the values available in array2
      System.out.println("Elements of Array2 is:");
      for (Object value : b2) {
         System.out.println("Value = " + value);
      }

      // initializing Object array3
      Integer[] b3 = new Integer[] { 1, 3 };

      // let us print all the values available in array3
      System.out.println("Elements of Array3 is:");
      for (Object value : b3) {
         System.out.println("Value = " + value);
      }

      // checking array1 and array2 for equality
      System.out.println("Array1 and Array2 are equal:" + Arrays.deepEquals(b1,b2));

      // checking array1 and array3 for equality
      System.out.println("Array1 and Array3 are equal:" + Arrays.deepEquals(b1,b3));
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

Elements of Array1 is:
Value = 1
Value = 2
Elements of Array2 is:
Value = 1
Value = 2
Elements of Array3 is:
Value = 1
Value = 3
Array1 and Array2 are equal:true
Array1 and Array3 are equal:false
java_util_arrays.htm
廣告