Arrays.mismatch(int[], int, int, int[],int, int) in Java

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

Syntax:

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

This method takes six arguments two of type int array and the rest four are of type int as its parameters. This method finds and returns the relative index of the first mismatch between two int arrays over the specified ranges, otherwise returns -1 if no mismatch is found.

Note: If the two arrays, over the specified ranges, share a common prefix then the returned relative index is the length of the common prefix.

Parameters: Six arguments are required for this method.

a: the first array to be tested for a mismatch.

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 a mismatch.

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: the relative index of the first mismatch between the two arrays over the specified ranges, otherwise -1.

Throws:

1. IllegalArgumentException - if aFromIndex > aToIndex orif 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 Arraysmismatchintrange {
    public static void main(String[] args) {

        int a[] = { 1, 2, 3, 5 };

        int b[] = { 2, 2, 3, 5, 6 };

        int aFromIndex = 1, aToIndex = 3;
        int bFromIndex = 1, bToIndex = 3;

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

Output:

-1


Approach 2: IllegalArgumentException 

Java

import java.util.Arrays;

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

        int a[] = { 1, 2, 3, 5 };

        int b[] = { 2, 2, 3, 5, 6 };

        int aFromIndex = 4, aToIndex = 3;
        int bFromIndex = 1, bToIndex = 3;

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

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(4) > toIndex(3) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.mismatch(Arrays.java:8035)


Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;

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

        int a[] = { 1, 2, 3, 5 };

        int b[] = { 2, 2, 3, 5, 6 };

        int aFromIndex = -1, aToIndex = 3;
        int bFromIndex = 1, bToIndex = 3;

        System.out.println(Arrays.mismatch(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.mismatch(Arrays.java:8035)


Approach 4: NullPointerException 

Java

import java.util.Arrays;

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

        int a[] = null;

        int b[] = { 2, 2, 3, 5, 6 };

        int aFromIndex = 1, aToIndex = 3;
        int bFromIndex = 1, bToIndex = 3;

        System.out.println(Arrays.mismatch(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.mismatch(Arrays.java:8035)



No comments:

Post a Comment