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



描述

Java Arrays equals(byte[] a, byte[] a2) 方法返回 true,如果兩個指定的位元組陣列彼此相等。如果兩個陣列包含相同順序的相同元素,則它們相等。如果兩個陣列引用均為 null,則它們被認為相等。

宣告

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

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

引數

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

  • a2 − 這是另一個要測試相等的陣列。

返回值

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

異常

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

描述

Java Arrays equals(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) 方法返回 true,如果給定範圍內的兩個位元組陣列彼此相等。如果任何陣列為 null,則丟擲 NullPointerException。

宣告

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

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three byte arrays
      byte[] arr1 = new byte[] { 10, 20, 30 };
      byte[] arr2 = new byte[] { 11, 22, 30 };
      byte[] arr3 = new byte[] { 10, 20, 30 };

      // 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(byte[], int, int, byte[], int, int) 方法的用法。首先,我們建立了三個位元組陣列,並使用 equals(byte[], int, int, byte[], int, int) 方法對它們進行了比較。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three byte arrays
      byte[] arr1 = new byte[] { 10, 20, 30 };
      byte[] arr2 = new byte[] { 11, 22, 30 };
      byte[] arr3 = new byte[] { 10, 20, 30 };

      // 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(byte[], int, int, byte[], int, int) 方法的用法。首先,我們建立了三個位元組陣列,並使用 equals(byte[], int, int, byte[], int, int) 方法比較了它們的子陣列。根據結果,打印出比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three byte arrays
      byte[] arr1 = new byte[] { 10, 20, 30 };
      byte[] arr2 = new byte[] { 11, 22, 30 };
      byte[] arr3 = new byte[] { 10, 20, 30 };

      // 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
廣告