Collections

Compare Arrays Java Code With Examples

Table of Contents

Program

import java.util.Arrays;
public class Arrayscompare
{ 
	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));
	}
}

Output

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, array2, 0, 3):-1
Arrays.compareUnsigned(array1, 0, 3, array2, 0, 3):0
Arrays.compareUnsigned(array1, 0, 3, array2, 0, 3):1

Description

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

Compares two int arrays lexicographically, numerically treating elements as unsigned.
If the two arrays share 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 respective arrays that is the prefix length. Otherwise, one array is a proper prefix of the other and, the lexicographic comparison is the result of comparing the two array lengths. (See mismatch(int[], int[]) for the definition of a common and proper prefix.)
A null array reference is considered lexicographically less than a non-null array reference. Two null array references are considered equal.

API Note: This method behaves as if (for non-null array references):

 int i = Arrays.mismatch(a, b);
 if (i >= 0 && i < Math.min(a.length, b.length))
     return Integer.compareUnsigned(a[i], b[i]);
 return a.length - b.length;

Parameters:

a – the first array to compare
b – the second array to compare

Returns:

the value 0 if the first and second array are equal and contain the same elements in the same order; a value less than 0 if the first array is lexicographically less than the second array; and a value greater than 0 if the first array is lexicographically greater than the second array

Since:

9

public static int compareUnsigned?(int[] a,int aFromIndex,

                             int aToIndex,
                              int[] b,
                              int bFromIndex,
                              int bToIndex)

Compares two int arrays lexicographically over the specified ranges, numerically treating elements as unsigned.
If the two arrays, over the specified ranges, share a common prefix then the lexicographic comparison is the result of comparing two elements, as if by Integer.compareUnsigned(int, int), at a relative index within the respective arrays that is the length of the prefix. Otherwise, one array is a proper prefix of the other and, the lexicographic comparison is the result of comparing the two range lengths. (See mismatch(int[], int, int, int[], int, int) for the definition of a common and proper prefix.)

API Note: This method behaves as if:

 int i = Arrays.mismatch(a, aFromIndex, aToIndex,
                         b, bFromIndex, bToIndex);
 if (i >= 0 && i < Math.min(aToIndex - aFromIndex, bToIndex - bFromIndex))
     return Integer.compareUnsigned(a[aFromIndex + i], b[bFromIndex + i]);
 return (aToIndex - aFromIndex) - (bToIndex - bFromIndex);

Parameters:

a – the first array to compare
aFromIndex – the index (inclusive) of the first element in the first array to be compared
aToIndex – the index (exclusive) of the last element in the first array to be compared
b – the second array to compare
bFromIndex – the index (inclusive) of the first element in the second array to be compared
bToIndex – the index (exclusive) of the last element in the second array to be compared

Returns:

the value 0 if, over the specified ranges, the first and second array are equal and contain the same elements in the same order; a value less than 0 if, over the specified ranges, the first array is lexicographically less than the second array; and a value greater than 0 if, over the specified ranges, the first array is lexicographically greater than the second array

Throws:

IllegalArgumentException – if aFromIndex > aToIndex or if bFromIndex > bToIndex
ArrayIndexOutOfBoundsException – if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length
NullPointerException – if either array is null

Since:

9