Java Arrays mismatch() Method
Example
Find the first position where two arrays are different:
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] myNum = {10, 20, 30, 40};
int[] myNum2 = {10, 20, 35, 40};
System.out.println(Arrays.mismatch(myNum, myNum2));
}
}
Definition and Usage
The mismatch() method compares two arrays item by item. It returns the index (position) of the first item that is different.
If the arrays are equal, it returns -1. If all items of the shorter array match the start of the longer array, it returns the length of the shorter array.
Syntax
public static int mismatch(type[] array1, type[] array2)
type can be boolean, byte, char, short, int, long, float, double or Object (for arrays of objects, such as String[]).
Parameter Values
| Parameter | Description |
|---|---|
| array1 | Required. The first array to compare. |
| array2 | Required. The second array to compare. |
Technical Details
| Returns: | The index of the first item that is different, or -1 if the arrays are equal. |
|---|---|
| Java version: | Java 9 |
Related Pages
❮ Arrays Methods