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



描述

Java Arrays equals(char[] a, char[] a2) 方法用於判斷兩個指定的字元陣列是否相等。如果兩個陣列包含相同的元素且順序相同,則它們被認為是相等的。如果兩個陣列引用都為 null,則它們也被認為是相等的。

宣告

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

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

引數

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

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

返回值

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

異常

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

描述

Java Arrays equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) 方法用於判斷兩個字元陣列在給定範圍內是否相等。如果任何一個數組為 null,則會丟擲 NullPointerException。

宣告

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

public static boolean equals​(char[] a, int aFromIndex, int aToIndex, char[] 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(char[], char[]) 方法的使用。首先,我們建立了三個字元陣列,並使用 equals(char[], char[]) 方法對它們進行了比較。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three char arrays
      char[] arr1 = new char[] { 'A', 'D' , 'F' };
      char[] arr2 = new char[] { 'B', 'E' , 'F' };
      char[] arr3 = new char[] { 'A', 'D' , 'F' };

      // 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);
   }
}

輸出

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

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

檢查字元子陣列是否相等示例

以下示例演示了 Java Arrays equals(char[], int, int, char[], int, int) 方法的使用。首先,我們建立了三個字元陣列,並使用 equals(char[], int, int, char[], int, int) 方法對它們進行了比較。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three char arrays
      char[] arr1 = new char[] { 'A', 'D' , 'F' };
      char[] arr2 = new char[] { 'B', 'E' , 'F' };
      char[] arr3 = new char[] { 'A', 'D' , 'F' };

      // 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);
   }
}

輸出

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

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

檢查字元子陣列是否相等示例

以下示例演示了 Java Arrays equals(char[], int, int, char[], int, int) 方法的使用。首先,我們建立了三個字元陣列,並使用 equals(char[], int, int, char[], int, int) 方法對它們的子陣列進行了比較。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three char arrays
      char[] arr1 = new char[] { 'A', 'D' , 'F' };
      char[] arr2 = new char[] { 'B', 'E' , 'F' };
      char[] arr3 = new char[] { 'A', 'D' , 'F' };

      // 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);
   }
}

輸出

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

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