Java Array mismatch() Method with Examples Last Updated : 15 Nov, 2024 Comments Improve Suggest changes Like Article Like Report The mismatch() method in Java is a utility from the java.util.Arrays class. It is used to compare two arrays element by element and determines the index of the first mismatch. It is quite useful to check whether two arrays contain the same corresponding elements or not. Note: If both arrays have corresponding elements same then this function returns -1. Mismatched Arrays: It is one of the most basic conditions where the values of arrays are different method responds with the array index where values are unequal. Java // Java program to demonstrate the working of // mismatch() method with arrays import java.util.Arrays; class Main { public static void main(String[] args){ int a[] = { 2, 7, 11, 22, 37 }; int b[] = { 2, 7, 19, 31, 39, 56 }; // Return the first index at which a // and b have the different element int i = Arrays.mismatch(a,b); System.out.println("Index mismatch for arrays: " + i); } } OutputIndex mismatch for arrays: 2 Syntax of mismatch() MethodArrays.mismatch( a , b );Parameters: The above method accepts the following parameters:a: first array name of a particular data type.b: second array name of the same type.Return value:-1: If both the arrays have same elements at all the corresponding positions.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 : For Identical Arrays Java // Java program to demonstrate the working of // mismatch() method with arrays of integer type import java.util.Arrays; class Main { public static void main(String[] args) { // Initializing an integer arrays int a[] = { 2, 7, 11, 22, 37 }; int b[] = { 2, 7, 11, 22, 37 }; // Return the first index at which a // and b have the different element int i = Arrays.mismatch(a,b); System.out.println("Index mismatch for arrays: " + i); } } OutputIndex mismatch for arrays: -1 Example 2: Arrays with Different LengthsIt seems that it work with int Array only but this is not the case we can use it with different data types and with different length too as mentioned below: Java // Java program to demonstrate the working of // mismatch() method with arrays of double type import java.util.Arrays; class Main { public static void main(String[] args) { // Initializing an array containing // double values double a[] = { 11.21, 22.31, 33.15, 44.18}; double b[] = { 11.21, 22.31, 33.15, 44.18}; double c[] = { 11.21, 22, 33, 44, 55, 66 }; // Return the first index at which a // and b have the different element int i1 = Arrays.mismatch(a,b); int i2 = Arrays.mismatch(a,c); System.out.println("Arrays are equal : " + i1); System.out.println("Arrays are not equal : " + i2); } } OutputArrays are equal : -1 Arrays are not equal : 1 It explains both the cases when the arrays have same values and unequal. Comment More infoAdvertise with us Next Article Java Array mismatch() Method with Examples bhuwanesh Follow Improve Article Tags : Java Java-Arrays Java-Array-Programs Practice Tags : Java Similar Reads Java Arrays compare() Method with Examples The Arrays compare() method in Java is a part of the java.util package to compare arrays lexicographically (dictionary order). This method is useful for ordering arrays and different overloads for different types including boolean, byte, char, double, float, int, long, short, and Object arrays. Exam 3 min read Java String compareTo() Method with Examples Strings in Java are objects that are supported internally by an array only which means contiguous allocation of memory for characters. Please note that strings are immutable in Java which means once we create a String object and assign some values to it, we cannot change the content. However, we can 7 min read Stack equals() method in Java with Example The Java.util.Stack.equals(Object obj) method of Stack class in Java is used verify the equality of an Object with a Stack and compare them. The list returns true only if both Stack contains same elements with same order. Syntax: first_Stack.equals(second_Stack) Parameters: This method accepts a man 2 min read Short equals() method in Java with Examples The equals() method of Short class is a built in method in Java which is used to compare the equality given Object with the instance of Short invoking the equals() method. Syntax ShortObject.equals(Object a) Parameters: It takes an Object type object a as input which is to be compared with the insta 2 min read MonthDay equals() method in Java with Examples The equals() method of MonthDay class in Java checks if this month-day is equal to another month-day. Syntax: public boolean equals(Object obj) Parameter: This method accepts a parameter obj which specifies if this month-day is equal to another month-day. Returns: The function returns true if this i 1 min read Writer equals() method in Java with Examples The Java.io.Writer.equals(Object obj) method of Writer class in Java is used to check whether the two instances of Writer are equal or not. It returns a boolean stating whether they are equal or not. Signature: public boolean equals(Writer second_Writer) Syntax: first_Writer.equals(second_Writer) Pa 2 min read NumberFormat equals() method in Java with Examples The equals() method is a built-in method of the java.text.NumberFormat accepts an argument which is an object and returns true if this argument object is same as the object, else it returns false. Syntax: public boolean equals(Object arg) Parameters: The function accepts a single mandatory parameter 2 min read Stream noneMatch() Method in Java with Examples A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. noneMatch() of Stream class returns whether no elements of this stream match the provided predicate. It may not evaluate the predicate on all elements if not necessary for determinin 3 min read UUID equals() Method in Java with Examples The equals() method of UUID class in Java is used to check for the equality of one UUID with another. The method returns True if both the UUID's are exactly identical bit by bit. Syntax: public boolean equals(Object uuidObj) Parameters: The method takes one parameter uuidObj to which the UUID_1 is t 2 min read SortedSet equals() method in Java with Examples The equals() method of java.util.SortedSet class is used to verify the equality of an Object with a SortedSet and compare them. The method returns true if the size of both the SortedSets are equal and both contain the same elements. Syntax: public boolean equals(Object o) Parameters: This method tak 2 min read Like