Java Arrays compare() Method with Examples
Arrays compare() method in Java comes under the Arrays class and java.util package. This method compares two arrays lexicographically (Dictionary order). There are two different versions of different overloads for Boolean, byte, char, double, float, int, long, short, and Object arrays. This method returns values as per the below-mentioned cases.
- It returns 0 if the array is equal to the other array.
- It returns a value less than 0 is returned if the array is lexicographically less than the other array in
- It returns a value greater than 0 if the array is lexicographically greater than the other array (more characters).
A null array is lexicographically less than a non-null array, and the two arrays are considered equal if both are null so that it will print 0 in this case.
Arrays.compare(array1,array2); // array1 and array2 are two arrays
Parameters and Return Type: The method compare() accepts an array as parameters with different data types example: string, integer, float, double, long, etc. The return type of this method is an integer. It returns a positive value if the array is lexicographically greater, negative if it is lesser, and 0 if equal.
Exceptions: It usually throws NullPointerException and ClassCastException, and both of these exceptions have different means too.
- NullPointerException: The NullPointerException is a runtime exception that refers to null and occurs when a variable is accessed that does not point to any object.
- ClassCastException: This exception occurs when we’re trying to convert one class object into another class-type object.
If we want to invoke this method, First import the “Arrays” class from JavaJava. util packages just by adding the “import java.util.Arrays” command. then invoke method by “Arrays” follow “.compare(parameter1,parameter2);” by this you can invoke compare() method easily.
Example 1: Let’s take an example, First import java.util.Arrays, create a public class named CompareExample, then Initialize two integer arrays with elements, compare them using the compare() method, and finally print the result from the compare method.
Result is 1
Hence the desired output is 1 because array1 is lexicographically greater than array2.
Example 2: Let’s take another example, In this example, we’ll take an array of different data types, and that is float, and do the same as we did in the example as mentioned above, first import class, create a new class named “CompareExample” then initialize two floating type array with elements then compare them using compare method of array class and finally print the result we got.
Result is -1
The result is -1 in output because floatArray1 is lexicographically less than floatArray2.
Example 3: Let’s take an example. In this, we’ll initialize an array with the same numbers and size. Then compare array1 and array2 and finally prints the result we got from compare method.
Result is 0
The desired output is 0 because we are given two integer arrays with the same numbers and size.
In this article, we’ve learned how to use the compare() method. If array1 is lexicographically greater than array2, it will return a positive(-ve) value. If array1 is lexicographically less than array2, it will return a negative(-ve) value. If both the array is lexicographically equal or null, then it will return 0.