Java Arrays.compareUnsigned()
方法
原文:https://www.studytonight.com/java-util/java-arrays-compareunsigned-method
在本教程中,我们将学习 Java 中Arrays
类的compareUnsigned()
方法。如果您不知道 compareUnsigned()方法,那么我们鼓励您阅读本文 Java 整数 compareUnsigned()方法。一个null
数组引用在字典序上被认为小于一个非null
数组引用。两个null
数组引用被认为是相等的。让我们看看这个方法的语法和例子。
句法
static int compareUnsigned(datatype[] array1, datatype[] array2)
compareUnsigned()
方法重载方法列表
此表包含compareUnsigned()
方法的所有重载变体。
方法 | 描述 |
---|---|
静态 int compareUnsigned(字节[] a,字节[] b) | 此方法按字典顺序比较双字节数组,在数字上将元素视为无符号的。 |
static int compareunsigned(字节[] a、int afromindex、int aToIndex、字节[] b、int bfromindex、int bToIndex) | 此方法在指定范围内按字典顺序比较双字节数组,在数字上将元素视为无符号的。 |
静态 int compareUnsigned(int[] a,int[] b) | 此方法按字典顺序比较两个 int 数组,在数字上将元素视为无符号的。 |
static int compare signed(int[]a、int aFromIndex、int aToIndex、int[] b、int bfromindex、int bToIndex) | 此方法在指定范围内按字典顺序比较两个 int 数组,在数字上将元素视为无符号的。 |
static int compare signed(long[]a、int afromindex、int aToIndex、long[] b、int bfromindex、int bToIndex) | 此方法在指定范围内按字典顺序比较两个长数组,在数字上将元素视为无符号的。 |
静态 int compareUnsigned(长[] a,长[] b) | 此方法按字典顺序比较两个长数组,在数字上将元素视为无符号的。 |
static int compare signed(short[]a、int afromindex、int aToIndex、short[] b、int bfromindex、int bToIndex) | 此方法在指定范围内按字典顺序比较两个短数组,在数字上将元素视为无符号的。 |
静态 int compareUnsigned(短[] a,短[] b) | 此方法按字典顺序比较两个短数组,在数字上将元素视为无符号的。 |
compareUnsigned()
示例
在下面给出的例子中,我们有三个 int 数组,我们使用compareUnsigned()
方法对它们进行比较。此方法按字典顺序比较两个 int 数组,在数字上将元素视为无符号的。如果两个数组有一个共同的前缀,那么字典式比较就是比较两个元素的结果,就像通过整数.比较符号(int,int) 在特定数组中的一个索引处比较前缀长度一样。相反,一个数组是另一个数组的适当前缀,字典式比较是比较两个数组范围的结果。
import java.util.Arrays;
class StudyTonight{
public static void main(String args[])
{
int[] array1 = {4, 8, 12, 16, 20};
int[] array2 = {4, 8, 12, 16, 20};
int[] array3 = {4, 8, 24, 16, 20};
System.out.println("array1: "+ Arrays.toString(array1));
System.out.println("array2: "+ Arrays.toString(array2));
System.out.println("array3: "+ Arrays.toString(array3));
System.out.println("\nArrays.compareUnsigned(array1, array2): " + Arrays.compareUnsigned(array1, array2));
System.out.println("Arrays.compareUnsigned(array1, array3): " + Arrays.compareUnsigned(array1, array3));
System.out.println("Arrays.compareUnsigned(array3, array1): " + Arrays.compareUnsigned(array3, array1));
System.out.println("\nArrays.compareUnsigned(array1, 0, 3, array3, 0, 3): " +
Arrays.compare(array1, 0, 3, array3, 0, 3));
System.out.println("Arrays.compareUnsigned(array1, 0, 3, array2, 0, 3): " +
Arrays.compare(array1, 0, 3, array2, 0, 3));
System.out.println("Arrays.compareUnsigned(array3, 0, 3, array1, 0, 3): " +
Arrays.compare(array3, 0, 3, array1, 0, 3));
}
}
array1: [4,8,12,16,20] array2: [4,8,12,16,20] array3: [4,8,24,16,20] T4】array . comparensigned(array 1,array 2):0 array . comparensigned(array 1,array3):-1 array . comparensigned(array 3,array 1):1 T8】array . comparensigned(array 1,array 3)
所有重载方法的compareUnsigned()
示例
下面给出的程序说明了 compareUnsigned()方法的所有重载方法。这种方法比较两个数组的对应元素,不考虑它们的正负。
import java.util.Arrays;
public class StudyTonight
{
public static void main(String[] args)
{
//Example of static int compareUnsigned(byte[] a, byte[] b)
byte[] byteArray1={4, -1, 8, 2, 2, 3, 16, 4, 4};
byte[] byteArray2={4, 1, -8, 2, 2, -3, 16, 4, 4};
int result = Arrays.compareUnsigned(byteArray1, byteArray2);
System.out.println("byteArray1 and byteArray2: "+result);
//Example of static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)
result = Arrays.compareUnsigned(byteArray1, 2, 4, byteArray2, 3, 5);
System.out.println("byteArray1 and byteArray2: "+result);
//Example of static int compareUnsigned(int[] a, int[] b)
int[] intArray1 ={5, -6, 1, 8, 5, 7, 1, 4};
int[] intArray2 ={5, 3, 7, 4, 3, 5, 7, 8};
result = Arrays.compareUnsigned(intArray1, intArray1);
System.out.println("intArray1 and intArray2: "+result);
//Example of static int compareUnsigned(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
result = Arrays.compareUnsigned(intArray1, 0, 4, intArray1, 0, 4);
System.out.println("intArray1 and intArray2: "+result);
//Example of static int compareUnsigned(long[] a, long[] b)
long[] longArray1 ={5, -6, 1, 8, 5, 7, 1, 4};
long[] longArray2 ={5, 3, 7, 4, 3, 5, 7, 8};
result = Arrays.compareUnsigned(longArray1, longArray2);
System.out.println("longArray1 and longArray2: "+result);
//Example of static int compareUnsigned(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)
result = Arrays.compareUnsigned(longArray1, 0, 4, longArray2, 0, 4);
System.out.println("longArray1 and longArray2: "+result);
//Example of static int compareUnsigned(short[] a, short[] b)
short[] shortArray1={5, 8, 6, 4, 2, 0};
short[] shortArray2={5, 2, 3, 4, 5, 6};
result = Arrays.compareUnsigned(shortArray1, shortArray2);
System.out.println("shortArray1 and shortArray2: "+result);
//Example of static int compareUnsigned(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)
result = Arrays.compareUnsigned(shortArray1, 0, 4, shortArray2, 0, 4);
System.out.println("shortArray1 and shortArray2: "+result);
}
}
字节数组 1 和字节数组 2: 254 字节数组 1 和字节数组 2: 6 数组 1 和数组 2: 0 数组 1 和数组 2: 0 长数组 1 和长数组 2: 1 长数组 1 和长数组 2: 1 短数组 1 和短数组 2: 6 短数组 1 和短数组 2: 6
结论:
在本教程中,我们学习了compareUnsigned()
方法,该方法按照字典顺序比较两个 int 数组,在数字上把元素视为无符号的。一个null
数组引用在字典上被认为不如一个非null
数组引用。两个null
数组引用被视为相等。