Java Arrays compareUnsigned() Method
In this tutorial, we will learn about compareUnsigned()
method of the Arrays
class in Java. If you don't know about the compareUnsigned() method then we encourage you to read this article Java Integer compareUnsigned() Method. A null
array reference is considered lexicographically less than a non-null
array reference. Two null
array references are considered equal. Let's see the syntax and examples of this method.
Syntax
static int compareUnsigned(datatype[] array1, datatype[] array2)
List of Overloading Methods of compareUnsigned()
Method
This table contains all the overloaded variants of compareUnsigned()
method.
Method |
Description |
static int compareUnsigned(byte[] a, byte[] b) |
This method compares two-byte arrays lexicographically, numerically treating elements as unsigned. |
static int compareUnsigned(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)
|
This method compares two-byte arrays lexicographically over the specified ranges, numerically treating elements as unsigned. |
static int compareUnsigned(int[] a, int[] b) |
This method compares two int arrays lexicographically, numerically treating elements as unsigned. |
static int compareUnsigned(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)
|
This method compares two int arrays lexicographically over the specified ranges, numerically treating elements as unsigned. |
static int compareUnsigned(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)
|
This method compares two long arrays lexicographically over the specified ranges, numerically treating elements as unsigned. |
static int compareUnsigned(long[] a, long[] b) |
This method compares two long arrays lexicographically, numerically treating elements as unsigned. |
static int compareUnsigned(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)
|
This method compares two short arrays lexicographically over the specified ranges, numerically treating elements as unsigned. |
static int compareUnsigned(short[] a, short[] b) |
This method compares two short arrays lexicographically, numerically treating elements as unsigned. |
Example of compareUnsigned()
In the example given below, we have three arrays of int and we are comparing them by using compareUnsigned()
method. This method compares two int arrays lexicographically, numerically treating elements as unsigned. If the two arrays have a common prefix then the lexicographic comparison is the result of comparing two elements, as if by Integer.compareUnsigned(int, int), at an index within the particular arrays that is the prefix length. Oppositely, one array is a proper prefix of the other and, the lexicographic comparison is the result of comparing the two array ranges.
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]
Arrays.compareUnsigned(array1, array2): 0
Arrays.compareUnsigned(array1, array3): -1
Arrays.compareUnsigned(array3, array1): 1
Arrays.compareUnsigned(array1, 0, 3, array3, 0, 3): -1
Arrays.compareUnsigned(array1, 0, 3, array2, 0, 3): 0
Arrays.compareUnsigned(array3, 0, 3, array1, 0, 3): 1
Example of compareUnsigned()
with all Overloading Methods
The program given below illustrates all the overloading methods of compareUnsigned() method. This method compares corresponding elements of two arrays without considering their negativity or positivity.
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);
}
}
byteArray1 and byteArray2: 254
byteArray1 and byteArray2: 6
intArray1 and intArray2: 0
intArray1 and intArray2: 0
longArray1 and longArray2: 1
longArray1 and longArray2: 1
shortArray1 and shortArray2: 6
shortArray1 and shortArray2: 6
Conclusion:
In this tutorial, we learned about the compareUnsigned()
method, This method compares two int arrays lexicographically, numerically treating elements as unsigned. A null
array reference is considered lexicographically less than a non-null
array reference. Two null
array references are considered equal.