Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Arrays mismatch() Method

In this tutorial, we will learn about mismatch() a method in Java. This method returns the first mismatched element index of an array. It is used to check whether two arrays contain similar elements or not and, inform when a mismatch occurs. Let's see its syntax and the examples to understand this method.

Syntax

mismatch(datatype[] array1, datatype[] array2)

This method returns the first mismatch element index for both arrays. If both arrays are the same then it will return -1.

List of the Overloading Methods of mismatch() Method

This table contains all the overloaded variants of mismatch() method.

Method Description

static int mismatch(boolean[] a, boolean[] b)

This method finds and returns the index of the first mismatch between two boolean arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two boolean arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(byte[] a, byte[] b)

This method finds and returns the index of the first mismatch between two-byte arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two-byte arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(char[] a, char[] b)

This method finds and returns the index of the first mismatch between two char arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two char arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(double[] a, double[] b)

This method finds and returns the index of the first mismatch between two double arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(double[] a, int aFromIndex, int

aToIndex, double[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two double arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(float[] a, float[] b)

This method finds and returns the index of the first mismatch between two float arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(float[] a, int aFromIndex, int

aToIndex, float[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two float arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(int[] a, int[] b)

This method finds and returns the index of the first mismatch between two int arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex)

Finds and returns the relative index of the first mismatch between two int arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two long arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(long[] a, long[] b)

This method finds and returns the index of the first mismatch between two long arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two short arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(short[] a, short[] b)

This method finds and returns the index of the first mismatch between two short arrays, otherwise, return -1 if no mismatch is found.

static int mismatch(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex)

This method finds and returns the relative index of the first mismatch between two Object arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static int mismatch(Object[] a, Object[] b)

This method finds and returns the index of the first mismatch between two Object arrays, otherwise, return -1 if no mismatch is found.

static <T> int mismatch(T[] a, int aFromIndex,int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp)

This method finds and returns the relative index of the first mismatch between two Object arrays over the specified ranges, otherwise, return -1 if no mismatch is found.

static <T> int mismatch(T[] a, T[] b, Comparator<? super T> cmp)

This method finds and returns the index of the first mismatch between two Object arrays, otherwise, return -1 if no mismatch is found.

Example: Arrays mismatch() Method

In the code given below, the mismatch() method receives two arguments, can see there are two arrays and the first three elements from the index 0 to index 2 are same in both arrays but at index 3, 20 is the element in array1 and 80 is the element in array2, therefore 3 is the first index where our arrays mismatched.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
        int array1[] = { 5, 10, 15, 20, 25 }; 
        int array2[] = { 5, 10, 15, 80, 100 }; 
        int index = Arrays.mismatch(array1, array2);
        System.out.println("mismatched index for both arrays is: "+index); 
	} 
}


mismatched index for both arrays is: 3

Example: Arrays mismatch for the particular range

In the following program, to check the first index at which elements of both arrays we passed both the arrays along with their ranges. In this array1 will be considered from the index 2 to 5 and array2 will be considered from the index 3 to 8.

import java.util.Arrays;
class StudyTonight { 
	public static void main(String args[]) 
	{ 
        int array1[] = { 15, 4, 22, 20, 45, 37, 18, 14, 12}; 
        int array2[] = { 7, 10, 11, 20, 45, 37, 18, 14, 12}; 
        int index = Arrays.mismatch(array1, 3, 5, array2, 3, 8 );
        System.out.println("mismatched index for both arrays is: "+index); 
	} 
}


mismatched index for both arrays is: 2

Example: Arrays mismatch() Overloading Methods

This method overloads for the following ways to support an array of different data types and for the different ranges of an array.

import java.util.Arrays;
public class StudyTonight 
{
	public static void main(String[] args) 
	{
		//Example of static int mismatch(boolean[] a, boolean[] b)
		boolean[] boolArray1={true, false, true, false, true, true};
		boolean[] boolArray2={true, false, false, true, true, true};
		int Index = Arrays.mismatch(boolArray1, boolArray2);
		System.out.println("boolArray1 and boolArray2 mismatched at index: "+Index);

		//Example of static int mismatch(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(boolArray1, 2, 4, boolArray2, 3, 5);
		System.out.println("boolArray1 and boolArray2 mismatched at index: "+Index);


		//Example of static int compare(byte[] a, byte[] b)
		byte[] byteArray1={4, 1, 8, 2, 2, 3, 16, 4, 4};
		byte[] byteArray2={4, 1, 8, 2, 2, 3, 16, 4, 4};
		Index = Arrays.mismatch(byteArray1, byteArray2);
		System.out.println("byteArray1 and byteArray2 mismatched at index: "+Index);

		Index = Arrays.mismatch(byteArray1, 2, 4, byteArray2, 3, 5);
		System.out.println("byteArray1 and byteArray2 mismatched at index: "+Index);


		//Example of static int mismatch(char[] a, char[] b)
		char[] charAarray1={'a', 'e', 'i', 'o', 'u'};
		char[] charAarray2={'a', 'e', 'p', 'q', 'r'};
		Index = Arrays.mismatch(charAarray1, charAarray2);
		System.out.println("charAarray1 and charAarray2 mismatched at index: "+Index);

		//Example of static int mismatch(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(charAarray1, 0, 4, charAarray2, 0, 4);
		System.out.println("charAarray1 and charAarray2 mismatched at index: "+Index);


		//Example of static int mismatch(double[] a, double[] b)
		double[] doubleArray1={12, 3, 17, 5, 8, 17, 2, 37};
		double[] doubleArray2={12, 3, 17, 5, 8, 17, 2, 50};
		Index = Arrays.mismatch(doubleArray1, doubleArray2);
		System.out.println("doubleArray1 and doubleArray2 mismatched at index: "+Index);

		//Example of static int mismatch(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(doubleArray2, 0, 4, doubleArray2, 0, 4);
		System.out.println("doubleArray1 and doubleArray2 mismatched at index: "+Index);	 

		//Example of static int mismatch(float[] a, float[] b)
		float[] floatArray1={4.12f, 7.3f, 8.17f, 1.5f, 7.8f, 4.17f, 3.2f, 6.37f};
		float[] floatArray2={4.12f, 8.3f, 5.17f, 6.5f, 4.8f, 6.17f, 2.2f, 5.37f};
		Index = Arrays.mismatch(floatArray1, floatArray2);
		System.out.println("floatArray1 and floatArray2 mismatched at index: "+Index);
		
		//Example of static int mismatch(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(floatArray1, 0, 4, floatArray2, 0, 4);
		System.out.println("floatArray1 and floatArray2 mismatched at index: "+Index);	
		
		
		//Example of static int mismatch(int[] a, int[] b)
		int[] intArray1 ={5, 6, 7, 10, 17, 7, 1, 4};		
		int[] intArray2 ={5, 6, 7, 10, 27, 5, 7, 8};		
		Index = Arrays.mismatch(intArray1, intArray2);
		System.out.println("intArray1 and intArray2 mismatched at index: "+Index);
		
		//Example of static int mismatch(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(intArray1, 0, 4, intArray2, 0, 4);
		System.out.println("intArray1 and intArray2 mismatched at index: "+Index);	
		

		//Example of static int mismatch(long[] a, long[] b)
		long[] longArray1={4,54,56,17,51,84,5,28,33,9};
		long[] longArray2={4,54,16,4,51,12,5,28,8,19};
		Index = Arrays.mismatch(longArray1, longArray1);
		System.out.println("longArray1 and longArray2 mismatched at index: "+Index);
		
		//Example of static int mismatch(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(longArray1, 0, 4, longArray1, 0, 4);
		System.out.println("longArray1 and longArray2 mismatched at index: "+Index);	


		//Example of static int mismatch(short[] a, short[] b)
		short[] shortArray1={5, 8, 6, 4, 2, 0};
		short[] shortArray2={5, 2, 3, 4, 5, 6};
		Index = Arrays.mismatch(shortArray1, shortArray2);
		System.out.println("shortArray1 and shortArray2  mismatched at index: "+Index);
		
		//Example of static int mismatch(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex)
		Index = Arrays.mismatch(shortArray1, 0, 4, shortArray2, 0, 4);
		System.out.println("shortArray1 and shortArray2 mismatched at index: "+Index);	
	}
}


boolArray1 and boolArray2 mismatched at index: 2
boolArray1 and boolArray2 mismatched at index: 1
byteArray1 and byteArray2 mismatched at index: -1
byteArray1 and byteArray2 mismatched at index: 0
charAarray1 and charAarray2 mismatched at index: 2
charAarray1 and charAarray2 mismatched at index: 2
doubleArray1 and doubleArray2 mismatched at index: 7
doubleArray1 and doubleArray2 mismatched at index: -1
floatArray1 and floatArray2 mismatched at index: 1
floatArray1 and floatArray2 mismatched at index: 1
intArray1 and intArray2 mismatched at index: 4
intArray1 and intArray2 mismatched at index: -1
longArray1 and longArray2 mismatched at index: -1
longArray1 and longArray2 mismatched at index: -1
shortArray1 and shortArray2 mismatched at index: 1
shortArray1 and shortArray2 mismatched at index: 1

Conclusion:

In this article, we learned how mismatch() method works with a different type of arrays to find the first index of both arrays where elements mismatched, If both the arrays are the same then this method will return -1. We want to compare two arrays in a particular range for that we can slice the arrays using the start index and end index for both the arrays using overloaded methods.