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



描述

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

宣告

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

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

引數

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

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

返回值

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

異常

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

描述

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

宣告

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

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three short arrays
      short[] arr1 = new short[] { 10, 15, 20 };
      short[] arr2 = new short[] { 12, 18, 20 };
      short[] arr3 = new short[] { 10, 15, 20 };

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three short arrays
      short[] arr1 = new short[] { 10, 15, 20 };
      short[] arr2 = new short[] { 12, 18, 20 };
      short[] arr3 = new short[] { 10, 15, 20 };

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

package com.tutorialspoint;

import java.util.Arrays;

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

      // initializing three short arrays
      short[] arr1 = new short[] { 10, 15, 20 };
      short[] arr2 = new short[] { 12, 18, 20 };
      short[] arr3 = new short[] { 10, 15, 20 };

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