Java Arrays equals(Object[], Object[]) 方法



描述

Java Arrays equals(Object[] a, Object[] a2) 方法用於判斷兩個指定的物件陣列是否相等。如果兩個物件 e1 和 e2 使用 Objects.equals(e1, e2) 判斷為相等,則認為它們相等。兩個陣列相等是指它們包含相同的元素,並且順序相同。如果兩個陣列引用都為 null,則認為它們相等。

宣告

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

public static boolean equals(Object[] a, Object[] a2)

引數

  • a − 要測試相等性的陣列。

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

返回值

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

異常

Java Arrays equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex) 方法

描述

Java Arrays equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex) 方法用於判斷兩個物件陣列在給定範圍內的部分是否相等。如果兩個物件 e1 和 e2 使用 Objects.equals(e1, e2) 判斷為相等,則認為它們相等。如果任何一個數組為 null,則會丟擲 NullPointerException 異常。

宣告

以下是 java.util.Arrays.compare(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex) 方法的宣告

public static boolean equals​(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)

引數

  • a − 第一個要測試相等性的陣列。

  • aFromIndex − 第一個陣列中要測試相等性的第一個元素的索引(包含)。

  • aToIndex − 第一個陣列中要測試相等性的最後一個元素的索引(不包含)。

  • b − 第二個要測試相等性的陣列。

  • bFromIndex − 第二個陣列中要測試相等性的第一個元素的索引(包含)。

  • bToIndex − 第二個陣列中要測試相等性的最後一個元素的索引(不包含)。

返回值

如果兩個陣列在指定範圍內相等,則此方法返回 true。

異常

  • IllegalArgumentException − 如果 aFromIndex > aToIndex 或 bFromIndex > bToIndex

  • ArrayIndexOutOfBoundsException − 如果 aFromIndex < 0 或 aToIndex > a.length 或 bFromIndex < 0 或 bToIndex > b.length

  • NullPointerException − 如果任一陣列為 null

檢查物件陣列是否相等示例

以下示例演示瞭如何使用 Java Arrays equals(Object[], Object[]) 方法。首先,我們建立了三個學生物件的陣列,並使用 equals(Object[], Object[]) 方法比較它們。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three Student arrays
      Student[] arr1 = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
      Student[] arr2 = { new Student(4, "Jene"), new Student(5, "John"), new Student(2, "Robert") };
      Student[] arr3 = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      // comparing arr1 and arr2
      boolean retval = Arrays.equals(arr1, arr2);
      System.out.println("arr1 and arr2 equal: " + retval);

      // comparing arr1 and arr3
      boolean retval2 = Arrays.equals(arr1, arr3);
      System.out.println("arr1 and arr3 equal: " + retval2);
   }
}
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);
   }
}

輸出

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

arr1 and arr2 equal: false
arr1 and arr3 equal: true

檢查物件子陣列是否相等示例

以下示例演示瞭如何使用 Java Arrays equals(Object[], int, int, Object[], int, int) 方法。首先,我們建立了三個學生物件的陣列,並使用 equals(Object[], int, int, Object[], int, int) 方法比較它們。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three Student arrays
      Student[] arr1 = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
      Student[] arr2 = { new Student(4, "Jene"), new Student(5, "John"), new Student(2, "Robert") };
      Student[] arr3 = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      // comparing arr1 and arr2
      boolean retval = Arrays.equals(arr1, 0, 2, arr2, 0, 2);
      System.out.println("arr1 and arr2 equal: " + retval);

      // comparing arr1 and arr3
      boolean retval2 = Arrays.equals(arr1, 0, 2, arr3, 0, 2);
      System.out.println("arr1 and arr3 equal: " + retval2);
   }
}
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);
   }
}

輸出

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

arr1 and arr2 equal: false
arr1 and arr3 equal: true

檢查物件子陣列是否相等示例

以下示例演示瞭如何使用 Java Arrays equals(Object[], int, int, Object[], int, int) 方法。首先,我們建立了三個學生物件的陣列,並使用 equals(Object[], int, int, Object[], int, int) 方法比較它們的子陣列。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three Student arrays
      Student[] arr1 = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
      Student[] arr2 = { new Student(4, "Jene"), new Student(5, "John"), new Student(2, "Robert") };
      Student[] arr3 = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };

      // comparing arr1 and arr2
      boolean retval = Arrays.equals(arr1, 2, 3, arr2, 2, 3);
      System.out.println("Last element of arr1 and arr2 equal: " + retval);

      // comparing arr1 and arr3
      boolean retval2 = Arrays.equals(arr1, 2, 3, arr3, 2, 3);
      System.out.println("Last element of arr1 and arr3 equal: " + retval2);
   }
}
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);
   }
}

輸出

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

Last element of arr1 and arr2 equal: true
Last element of arr1 and arr3 equal: true
java_util_arrays.htm
廣告