Open In App

Compare Two Arrays in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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
// Comparison of Arrays
// Using == Operators
 
// Importing required classes
import java.util.*;
 
// Main class
class 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");
    }
}


Output

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”.

How to Compare Array Contents? 

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 classes
import java.util.Arrays;
 
// Main class
class 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");
    }
}


Output

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.

How to Deep Compare Array Contents?   

Example 1-A:

Java




// Java Program to Check If Two Arrays Are Equal
// Using equals() method of Arrays class
 
// Importing required classes
import java.util.Arrays;
 
// Main class
class 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");
    }
}


Output

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 classes
import java.util.Arrays;
 
// Main class
class 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");
    }
}


Output

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 classes
import java.util.Arrays;
 
// Main class
class 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");
    }
}


Output

Same


Last Updated : 31 May, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads