Compare Two Arrays in Java - GeeksforGeeks

24 Apr, 2022
While comparing two arrays we can not use “==” operator as it will compare the addresses of the memory block to which both the arrays are pointing. If we want to compare the elements inside the array we need to figure out other ways instead of using arithmetic operators. As we all know arrays data structures possess the property of containing elements in a continuous manner because of which we can calculate the size of both the arrays using the size() method of Arrays class itself, and can start comparing the indices if the size of both arrays is found to be equal.
Illustration:
Java
// Java Program to Illustrate// Comparision of Arrays// Using == Operators // Importing required classesimport java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Declaring integer arrays and        // initializing them with custom values        int arr1[] = { 1, 2, 3 };        int arr2[] = { 1, 2, 3 };         // Comparing arrays if equal or not        // using == operator        if (arr1 == arr2)             // Print statement            System.out.println("Same");        else             // Print statement            System.out.println("Not same");    }}
Not same
Output explanation: In Java, arrays are first class objects. In the above program, arr1 and arr2 are two references to two different objects. So when we compare arr1 and arr2, two reference variables are compared, therefore we get the output as “Not Same”.
A simple way is to run a loop and compare elements one by one. Java provides a direct method Arrays.equals() to compare two arrays. Actually, there is a list of equals() methods in the Arrays class for different primitive types (int, char, ..etc) and one for Object type (which is the base of all classes in Java). 
Example:
Java
// Java Program to Check If Two Arrays Are Equal// Using equals() method of Arrays class // Importing required classesimport java.util.Arrays; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Declaring integer arrays        int arr1[] = { 1, 2, 3 };        int arr2[] = { 1, 2, 3 };         // Checking if above two arrays are equal        // using equals() method        if (Arrays.equals(arr1, arr2))             // Print statement if arrays are equal            System.out.println("Same");        else             // Print statement if arrays are equal            System.out.println("Not same");    }}
Same
Output explanation: As seen above, the Arrays.equals() works fine and compares arrays contents. Now the question, what if the arrays contain arrays inside them or some other references which refer to different objects but have the same values. For example, refer to the below program as follows.
Example 1-A:
Java
// Java Program to Check If Two Arrays Are Equal// Using equals() method of Arrays class // Importing required classesimport java.util.Arrays; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {        // Declaring and initializing integer arrays        // having same values         // Array 1        int inarr1[] = { 1, 2, 3 };        // // Array 2        int inarr2[] = { 1, 2, 3 };         // // Array 1 contains only one element        Object[] arr1 = { inarr1 };        // Array 2 also contains only one element        Object[] arr2 = { inarr2 };         // Checking if arrays are equal or not        // using equals() method        if (Arrays.equals(arr1, arr2))             // Print statement if arrays are same            System.out.println("Same");        else             // Print statement if arrays are not same            System.out.println("Not same");    }}
Not same
Output explanation: So Arrays.equals() is not able to do a deep comparison. Java provides another method for this Arrays.deepEquals() which does the deep comparison.
Example 1-B:
Java
// Java Program to Check If Two Arrays Are Equal// Using deepEquals() method of Arrays class // Importing required classesimport java.util.Arrays; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Declaring and initializing integer arrays        // having same values         // Array 1        int inarr1[] = { 1, 2, 3 };        // Array 2        int inarr2[] = { 1, 2, 3 };         // Array 1 contains only one element        Object[] arr1 = { inarr1 };        // Array 2 also contains only one element        Object[] arr2 = { inarr2 };         // Checking if arrays are equal or not        // using deepEquals() method        if (Arrays.deepEquals(arr1, arr2))             // Print statement if arrays are same            System.out.println("Same");        else             // Print statement if arrays are not same            System.out.println("Not same");    }}
Same
Output Explanation: Let us now understand how does deepEquals() method of the Arrays class work internally. It compares two objects using any custom equals() methods they may have (if they have an equals() method implemented other than Object.equals()). If not, this method will then proceed to compare the objects field by field, recursively. As each field is encountered, it will attempt to use the derived equals() if it exists, otherwise, it will continue to recurse further. 
This method works on a cyclic Object graph like this: A->B->C->A. It has cycle detection so ANY two objects can be compared, and it will never enter into an endless loop.
Example 1-C:  
Java
// Java Program to Check If Two Arrays Are Equal// Using deepEquals() method of Arrays class // Importing required classesimport java.util.Arrays; // Main classclass GFG {     // Main driver method    public static void main(String[] args)    {         // Declaring and initializing integer arrays        // having same values         // Array 1        int inarr1[] = { 1, 2, 3 };        // Array 2        int inarr2[] = { 1, 2, 3 };         // Array 1 contains only one element        Object[] arr1 = { inarr1 };        // Array 2 also contains only one element        Object[] arr2 = { inarr2 };         // outarr1 contains only one element        Object[] outarr1 = { arr1 };        // outarr2 also contains only one element        Object[] outarr2 = { arr2 };         // Checking if arrays are equal or not        // using deepEquals() method        if (Arrays.deepEquals(outarr1, outarr2))             // Print statement if arrays are same            System.out.println("Same");        else             // Print statement if arrays are not same            System.out.println("Not same");    }}
Same
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Java-Array-Programs
Java-Arrays
Java
School Programming
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Split() String method in Java with examples
Arrays.sort() in Java with examples
Initialize an ArrayList in Java
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects