Arrays.compareUnsigned(long[], int, int, long[], int, int) in Java

Arrays.compareUnsigned(long[], int, int, long[], int, int): This method is available in java.util.Arrays class of Java.

Syntax:

int java.util.Arrays.compareUnsigned(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

This method takes six arguments, two are of type long array and the rest four are of type int as its parameter. This method compares two long arrays lexicographically over the specified ranges, numerically treating elements as unsigned.

Parameters: Six parameters are required for this method.

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:

1. The value 0 if, over the specified ranges, the first and second arrays are equal and contain the same elements in the same order.

2. A value less than 0 if, over the specified ranges, the first array is lexicographically less than the second array.

3. A value greater than 0 if, over the specified ranges, the first array is lexicographically greater than the second array.

Throws:

1. IllegalArgumentException - if aFromIndex > aToIndex or if bFromIndex > bToIndex.

2. ArrayIndexOutOfBoundsException - if aFromIndex < 0 or aToIndex > a.length or if bFromIndex < 0 or bToIndex > b.length.

3. NullPointerException - if either array is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;

public class ArrayscompareUnsignedlongRange {

    public static void main(String[] args) {

        long longArraya[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        long longArrayb[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        int aFromIndex = 2, aToIndex = 4;
        int bFromIndex = 2, bToIndex = 4;

        System.out.println(Arrays.compareUnsigned(longArraya,
aFromIndex, aToIndex, longArrayb,
bFromIndex, bToIndex));
    }
}

Output:

0


Approach 2: IllegalArgumentException

Java

import java.util.Arrays;

public class ArrayscompareUnsignedlongRange {

    public static void main(String[] args) {

        long longArraya[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        long longArrayb[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        int aFromIndex = 5, aToIndex = 4;
        int bFromIndex = 2, bToIndex = 4;

        System.out.println(Arrays.compareUnsigned(longArraya,
aFromIndex, aToIndex, longArrayb,
bFromIndex, bToIndex));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(4) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.compareUnsigned(Arrays.java:6815)



Approach 3: ArrayIndexOutOfBoundsException

Java

import java.util.Arrays;

public class ArrayscompareUnsignedlongRange {

    public static void main(String[] args) {

        long longArraya[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        long longArrayb[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        int aFromIndex = 2, aToIndex = 7;
        int bFromIndex = 2, bToIndex = 4;

        System.out.println(Arrays.compareUnsigned(longArraya,
aFromIndex, aToIndex, longArrayb,
bFromIndex, bToIndex));
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 7 at java.base/java.util.Arrays.rangeCheck(Arrays.java:725) at java.base/java.util.Arrays.compareUnsigned(Arrays.java:6815)


Approach 4: NullPointerException 

Java

import java.util.Arrays;

public class ArrayscompareUnsignedlongRange {

    public static void main(String[] args) {

        long longArraya[] = null;
        long longArrayb[] = { 17177, 38199, 499999,
5919911, 698191, 70000989 };
        int aFromIndex = 2, aToIndex = 4;
        int bFromIndex = 2, bToIndex = 4;

        System.out.println(Arrays.compareUnsigned(longArraya,
aFromIndex, aToIndex, longArrayb,
bFromIndex, bToIndex));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "a" is null at java.base/java.util.Arrays.compareUnsigned(Arrays.java:6815)



No comments:

Post a Comment