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

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

Syntax:

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

This method takes six arguments, two of type boolean array and the rest four are of type int as its parameters. This method returns true if the two specified arrays of booleans, 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, areequal.

Throws:

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

2. ArrayIndexOutOfBoundsException - if aFromIndex < 0 or aToIndex > a.length orif 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 Arraysequalsbooleanrange {
    public static void main(String[] args) {

        boolean a[] = { true, true, false,
true, true, false };

        int aFromIndex = 2, aToIndex = 4;

        boolean b[] = { false, false, true,
true, true };

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

        boolean a[] = { true, true, false,
true, true, false };

        int aFromIndex = 5, aToIndex = 4;

        boolean b[] = { false, false, true,
true, true };

        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:2773)


Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;

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

        boolean a[] = { true, true, false,
true, true, false };

        int aFromIndex = -2, aToIndex = 4;

        boolean b[] = { false, false, true,
true, true };

        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: -2 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.equals(Arrays.java:2773)


Approach 4: NullPointerException 

Java

import java.util.Arrays;

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

        boolean a[] = null;

        int aFromIndex = 2, aToIndex = 4;

        boolean b[] = { false, false, true,
true, true };

        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:2773)


No comments:

Post a Comment