Java 陣列 equals(long[], long[]) 方法



描述

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

宣告

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

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

引數

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

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

返回值

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

異常

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

描述

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

宣告

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

public static boolean equals​(long[] a, int aFromIndex, int aToIndex, long[] 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

檢查 long 型陣列是否相等示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

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

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

檢查 long 型陣列是否相等示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

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

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

檢查 long 型子陣列是否相等示例

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

package com.tutorialspoint;

import java.util.Arrays;

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

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

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