Java Array mismatch() Method with Examples
Last Updated :
16 Nov, 2021
The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of Object class. The methods of this class can be used by the class name itself.
The mismatch() is a method that is defined under the Arrays class of Java.util package and it is used with respect to the two arrays passed as an argument in the mismatch method. This method returns the index at which two arrays passed as a parameter to the mismatch() function have the first unequal element. It is quite useful to check whether two arrays contain the same corresponding elements or not. This responds when a mismatch occurs. If both arrays have corresponding elements same then this function returns -1. We can understand its working by considering the following example:
We are given two arrays, array1 = {2 , 6 , 1 , 10} and array2 = {2 , 6 , 11 , 12} and we want to find the index at which array1 and array2 have first unequal element. As the first two indices have the same set of corresponding elements so the index is 2.
We can achieve the above task without iterating over the arrays with the help of mismatch() method.
Syntax:
Arrays.mismatch(first_array, second_array);
Parameters: The above method accepts the following parameters:
1. first_array: An array (first array name) of a particular data type.
2. second_array: Another array (second array name) of the same type.
Return value:
1. -1: If both the arrays have same elements at all the corresponding positions.
2. non-negative integer: The index at which both the arrays have first unequal elements.
Note: The data type of both arrays must be the same, and this method follows zero-based indexing.
Example 1:
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
int array1[] = { 2 , 7 , 11 , 22 , 37 };
int array2[] = { 2 , 7 , 11 , 22 , 37 };
int array3[] = { 2 , 7 , 19 , 31 , 39 , 56 };
int index1 = Arrays.mismatch(array1, array2);
int index2 = Arrays.mismatch(array1, array3);
int index3 = Arrays.mismatch(array2, array3);
System.out.println(
"The index at which array1 and array2 have first unequal element: "
+ index1);
System.out.println(
"The index at which array1 and array3 have first unequal element: "
+ index2);
System.out.println(
"The index at which array2 and array3 have first unequal element: "
+ index3);
}
}
|
Output
The index at which array1 and array2 have first unequal element: -1
The index at which array1 and array3 have first unequal element: 2
The index at which array2 and array3 have first unequal element: 2
Example 2:
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
double array1[]
= { 11.21 , 22.31 , 33.15 , 44.18 , 55.19 , 66.666 };
double array2[]
= { 11.21 , 22.31 , 33.15 , 44.18 , 55.19 , 66.666 };
double array3[] = { 11.21 , 22 , 33 , 44 , 55 , 66 };
int index1 = Arrays.mismatch(array1, array2);
int index2 = Arrays.mismatch(array1, array3);
int index3 = Arrays.mismatch(array2, array3);
System.out.println(
"The index at which array1 and array2 have first unequal element: "
+ index1);
System.out.println(
"The index at which array1 and array3 have first unequal element:"
+ index2);
System.out.println(
"The index at which array2 and array3 have first unequal element: "
+ index3);
}
}
|
Output
The index at which array1 and array2 have first unequal element: -1
The index at which array1 and array3 have first unequal element:1
The index at which array2 and array3 have first unequal element: 1
Example 3:
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
char array1[] = { 'g' , 'e' , 'e' , 'k' , 's' };
char array2[] = { 'g' , 'e' , 'e' , 'k' , 's' };
char array3[] = { 'g' , 'e' , 'e' , 'k' };
int index1 = Arrays.mismatch(array1, array2);
int index2 = Arrays.mismatch(array1, array3);
int index3 = Arrays.mismatch(array2, array3);
System.out.println(
"The index at which array1 and array2 have first unequal element: "
+ index1);
System.out.println(
"The index at which array1 and array3 have first unequal element: "
+ index2);
System.out.println(
"The index at which array2 and array3 have first unequal element: "
+ index3);
}
}
|
Output
The index at which array1 and array2 have first unequal element: -1
The index at which array1 and array3 have first unequal element: 4
The index at which array2 and array3 have first unequal element: 4
Example 4:
Java
import java.io.*;
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
boolean array1[] = { true , false , true , false };
boolean array2[] = { true , false , true , false };
boolean array3[] = { true , false , false , true };
int index1 = Arrays.mismatch(array1, array2);
int index2 = Arrays.mismatch(array1, array3);
int index3 = Arrays.mismatch(array2, array3);
System.out.println(
"The index at which array1 and array2 have first unequal element: "
+ index1);
System.out.println(
"The index at which array1 and array3 have first unequal element: "
+ index2);
System.out.println(
"The index at which array2 and array3 have first unequal element: "
+ index3);
}
}
|
Output
The index at which array1 and array2 have first unequal element: -1
The index at which array1 and array3 have first unequal element: 2
The index at which array2 and array3 have first unequal element: 2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...