Arrays.mismatch(boolean[], int, int, boolean[], int, int): This method is available in java.util.Arrays class of Java.
Syntax:
int java.util.Arrays.mismatch(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)
This method takes six arguments two are of type boolean 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 boolean 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 parameters 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 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 Arraysmismatchbooleanrange {public static void main(String[] args) {boolean a[] = { true, false, false, true };boolean b[] = { false, false, false, true, false };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 Arraysmismatchbooleanrange {public static void main(String[] args) {boolean a[] = { true, false, false, true };boolean b[] = { false, false, false, true, false };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:7527)
Approach 3: ArrayIndexOutOfBoundsException
Java
import java.util.Arrays;public class Arraysmismatchbooleanrange {public static void main(String[] args) {boolean a[] = { true, false, false, true };boolean b[] = { false, false, false, true, false };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:7527)
Approach 4: NullPointerException
Java
import java.util.Arrays;public class Arraysmismatchbooleanrange {public static void main(String[] args) {boolean a[] = null;boolean b[] = { false, false, false, true, false };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:7527)
No comments:
Post a Comment