Java Arrays compareUnsigned() 方法



描述

Java Arrays compareUnsigned(short[] a, short[] b) 方法按字典順序比較兩個短整型陣列,數值上將它們視為無符號數。如果陣列為空,則認為陣列相等,空陣列按字典順序小於非空陣列。

宣告

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

public static int compareUnsigned(short[] a, short[] b)

引數

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

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

返回值

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

異常

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

描述

Java Arrays compareUnsigned(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex) 方法按字典順序比較給定範圍內的兩個短整型陣列,數值上將它們視為無符號數。如果任何陣列為空,則丟擲 NullPointerException。

宣告

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

public static int compareUnsigned​(short[] a, int aFromIndex, int aToIndex, short[] 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 − 如果任一陣列為空

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

以下示例顯示了 Java Arrays compareUnsigned(short[], short[]) 方法的用法。首先,我們建立了兩個具有相同 short 值的陣列,並使用 compareUnsigned() 方法比較它們。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initialize first short array
      short array1[] = { 5, 10, 3, 14, 23 };

      // initialize second short array
      short array2[] = { 5, 10, 3, 14, 23 };
            
      int result = Arrays.compareUnsigned(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.

比較兩個 short 值的子陣列示例

以下示例顯示了 Java Arrays compareUnsigned​(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex) 方法的用法。首先,我們建立了兩個具有不同 short 值的陣列,並使用 compareUnsigned() 方法比較它們。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initialize first short array
      short array1[] = { 5, 10, 3, 14, 23 };

      // initialize second short array
      short array2[] = { 5, 15, 3, 14, 23 };
      
      int result = Arrays.compareUnsigned(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.

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

以下示例顯示了 Java Arrays compareUnsigned(short[], short[]) 方法的用法。首先,我們建立了兩個具有不同 short 值的陣列,並使用 compareUnsigned() 方法比較它們。根據結果,列印比較結果。

package com.tutorialspoint;

import java.util.Arrays;

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

      // initialize first short array
      short array1[] = {  5, 15, 3, 14, 23 };

      // initialize second short array
      short array2[] = {  5, 10, 3, 14, 23 };
      
      int result = Arrays.compareUnsigned(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
廣告