Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java Array mismatch() Method with Examples
Next article icon

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);      } } 

Output
Index mismatch for arrays: 2 

Syntax of mismatch() Method

Arrays.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);      } } 

Output
Index mismatch for arrays: -1 

Example 2: Arrays with Different Lengths

It 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);      } } 

Output
Arrays are equal : -1 Arrays are not equal : 1 

It explains both the cases when the arrays have same values and unequal.


Next Article
Java Array mismatch() Method with Examples

B

bhuwanesh
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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences