Java Arrays compare() 方法



描述

Java Arrays compare(boolean[] a, boolean[] b) 方法按字典順序比較兩個布林型陣列。如果陣列為空,則認為陣列相等,並且空陣列按字典順序小於非空陣列。

宣告

以下是 java.util.Arrays.compare(boolean[] a, boolean[] b) 方法的宣告

public static int compare(boolean[] a, boolean[] b)

引數

  • a − 要比較的第一個陣列。

  • b − 要比較的第二個陣列。

返回值

如果第一個和第二個陣列相等,並且包含相同順序的相同元素,則此方法返回 0;如果第一個陣列按字典順序小於第二個陣列,則返回小於 0 的值;如果第一個陣列按字典順序大於第二個陣列,則返回大於 0 的值。

異常

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

描述

Java Arrays compare(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex) 方法按字典順序比較給定範圍內的兩個布林型陣列。如果任何陣列為空,則丟擲 NullPointerException。

宣告

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

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

引數

  • a − 要比較的第一個陣列。

  • aFromIndex − 要比較的第一個陣列的第一個元素(包含)的索引。

  • aToIndex − 要比較的第一個陣列的最後一個元素(不包含)的索引。

  • b − 要比較的第二個陣列。

  • bFromIndex − 要比較的第二個陣列的第一個元素(包含)的索引。

  • bToIndex − 要比較的第二個陣列的最後一個元素(不包含)的索引。

返回值

如果在指定範圍內,第一個和第二個陣列相等,並且包含相同順序的相同元素,則此方法返回 0;如果在指定範圍內,第一個陣列按字典順序小於第二個陣列,則返回小於 0 的值;如果在指定範圍內,第一個陣列按字典順序大於第二個陣列,則返回大於 0 的值。

異常

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

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

  • NullPointerException − 如果任一陣列為空

比較兩個具有相同布林值的陣列示例

以下示例演示了 Java Arrays compare(boolean[], boolean[]) 方法的使用。首先,我們建立了兩個具有相同布林值的陣列,並使用 compare() 方法對其進行比較。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initialize first boolean array
      boolean array1[] = { false, false, true, true, false};

      // initialize second boolean array
      boolean array2[] = { false, false, true, true, false};
            
      int result = Arrays.compare(array1, array2);
      
      if(result > 0) {
    	  System.out.println("First array is greater than second array.");
      } else if (result == 0) {
    	  System.out.println("Arrays are same.");
      } else {
    	  System.out.println("First array is less than second array.");
      }
   }
}

輸出

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

Arrays are same.

比較兩個布林值子陣列的示例

以下示例演示了 Java Arrays compare​(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex) 方法的使用。首先,我們建立了兩個具有不同布林值的陣列,並使用 compare() 方法對其進行比較。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initialize first boolean array
      boolean array1[] = { false, false, true, true, false};

      // initialize second boolean array
      boolean array2[] = { false, true, true, true, false};
      
      int result = Arrays.compare(array1, 0, 2, array2, 0, 2);
      
      if(result > 0) {
    	  System.out.println("First array is greater than second array.");
      } else if (result == 0) {
    	  System.out.println("Arrays are same.");
      } else {
    	  System.out.println("First array is less than second array.");
      }
   }
}

輸出

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

First array is less than second array.

比較兩個具有不同布林值的陣列的示例

以下示例演示了 Java Arrays compare(boolean[], boolean[]) 方法的使用。首先,我們建立了兩個具有不同布林值的陣列,並使用 compare() 方法對其進行比較。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initialize first boolean array
      boolean array1[] = { false, true, true, true, false};

      // initialize second boolean array
      boolean array2[] = { false, false, true, true, false};
      
      int result = Arrays.compare(array1, array2);
      
      if(result > 0) {
    	  System.out.println("First array is greater than second array.");
      } else if (result == 0) {
    	  System.out.println("Arrays are same.");
      } else {
    	  System.out.println("First array is less than second array.");
      }
   }
}

輸出

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

First array is greater than second array.
java_util_arrays.htm
廣告