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

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

Syntax:

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

This method takes six arguments two of type long array and the rest four are of type int as its parameters. This method returns true if the two specified arrays of longs, over the specified ranges, are equal to one another.

Parameters: Six parameters are required for this method.

a: the first array to be tested for equality.

aFromIndex: the index (inclusive) of the first element in the first array to be tested.

aToIndex: the index (exclusive) of the last element in the first array to be tested.

b: the second array to be tested for equality.

bFromIndex: the index (inclusive) of the first element in the second array to be tested.

bToIndex: the index (exclusive) of the last element in the second array to be tested.

Returns: true if the two arrays, over the specified ranges, are equal.

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 Arraysequalslongrange {
    public static void main(String[] args) {

        long a[] = { 1, 1, 0, 1, 1, 0 };

        int aFromIndex = 2, aToIndex = 4;

        long b[] = { 0, 0, 1, 1, 1 };

        int bFromIndex = 1, bToIndex = 3;

        System.out.println(Arrays.equals(a, aFromIndex,
aToIndex, b, bFromIndex, bToIndex));
    }
}

Output:

true


Approach 2: IllegalArgumentException 

Java

import java.util.Arrays;

public class Arraysequalslongrange {
    public static void main(String[] args) {

        long a[] = { 1, 1, 0, 1, 1, 0 };

        int aFromIndex = 5, aToIndex = 4;

        long b[] = { 0, 0, 1, 1, 1 };

        int bFromIndex = 1, bToIndex = 3;

        System.out.println(Arrays.equals(a, aFromIndex,
aToIndex, b, 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.equals(Arrays.java:2411)



Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;

public class Arraysequalslongrange {
    public static void main(String[] args) {

        long a[] = { 1, 1, 0, 1, 1, 0 };

        int aFromIndex = -1, aToIndex = 4;

        long b[] = { 0, 0, 1, 1, 1 };

        int bFromIndex = 1, bToIndex = 3;

        System.out.println(Arrays.equals(a, aFromIndex,
aToIndex, b, bFromIndex, bToIndex));
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.equals(Arrays.java:2411)



Approach 4: NullPointerException

Java

import java.util.Arrays;

public class Arraysequalslongrange {
    public static void main(String[] args) {

        long a[] = null;

        int aFromIndex = 2, aToIndex = 4;

        long b[] = { 0, 0, 1, 1, 1 };

        int bFromIndex = 1, bToIndex = 3;

        System.out.println(Arrays.equals(a, aFromIndex,
aToIndex, b, 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.equals(Arrays.java:2411)



No comments:

Post a Comment