Sunday, November 17, 2024
Google search engine
HomeLanguagesJavaJava Array mismatch() Method with Examples

Java Array mismatch() Method with Examples

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




// Java program to demonstrate the working of
// mismatch() method with arrays of integer type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing an integer array
        int array1[] = { 2, 7, 11, 22, 37 };
  
        // Initializing another array
        int array2[] = { 2, 7, 11, 22, 37 };
  
        // Initializing another array
        int array3[] = { 2, 7, 19, 31, 39, 56 };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element: "
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element: "
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        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




// Java program to demonstrate the working of
// mismatch() method with arrays of double type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing an array containing double values
        double array1[]
            = { 11.21, 22.31, 33.15, 44.18, 55.19, 66.666 };
  
        // Initializing another array
        double array2[]
            = { 11.21, 22.31, 33.15, 44.18, 55.19, 66.666 };
  
        // Initializing another array
        double array3[] = { 11.21, 22, 33, 44, 55, 66 };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element: "
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element:"
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        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




// Java program to demonstrate the working of
// mismatch() method with arrays of character type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing an character array
        char array1[] = { 'g', 'e', 'e', 'k', 's' };
  
        // Initializing another array
        char array2[] = { 'g', 'e', 'e', 'k', 's' };
  
        // Initializing another array
        char array3[] = { 'g', 'e', 'e', 'k' };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element: "
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element: "
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        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




// Java program to demonstrate the working of
// mismatch() method with arrays of boolean type
  
// Including necessary import statements
import java.io.*;
import java.util.Arrays;
  
class GFG {
    public static void main(String[] args)
    {
  
        // Initializing a boolean array
        boolean array1[] = { true, false, true, false };
  
        // Initializing another array
        boolean array2[] = { true, false, true, false };
  
        // Initializing another array
        boolean array3[] = { true, false, false, true };
  
        // Return the first index at which array1
        // array2 have the different element
        int index1 = Arrays.mismatch(array1, array2);
  
        // Return the first index at which array1
        // array3 have the different element
        int index2 = Arrays.mismatch(array1, array3);
  
        // Return the first index at which array2
        // array3 have the different element
        int index3 = Arrays.mismatch(array2, array3);
  
        // Print the first index at which array1
        // array2 have the different element
        System.out.println(
            "The index at which array1 and array2 have first unequal element: "
            + index1);
  
        // Print the first index at which array1
        // array3 have the different element
        System.out.println(
            "The index at which array1 and array3 have first unequal element: "
            + index2);
  
        // Print the first index at which array2
        // array3 have the different element
        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
RELATED ARTICLES

Most Popular

Recent Comments