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