How to Compare Two Arrays in Java

Comparing two arrays in Java can be tricky because the “==” operator only checks if two array variables reference the same memory location. To compare the actual contents of two arrays, you need to use methods provided by the Arrays class, such as Arrays.equals() and Arrays.deepEquals().

Let’s explore different approaches to effectively compare arrays in Java.

Comparing Arrays with the “==” Operator

Using the “==” operator with arrays compares memory addresses, not content.

// Demonstrates array comparison using the "==" operator

import java.util.*;

class CompareArrays {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {1, 2, 3};

        if (arr1 == arr2) {
            System.out.println("Same"); 
        } else {
            System.out.println("Not same"); // This will be the output
        }
    }
}

Output:

Not same

Explanation: Even though arr1 and arr2 contain the same elements, they are distinct objects in memory. Therefore, “==” returns false.

Comparing Arrays with Arrays.equals()

The Arrays.equals() method compares the contents of two arrays. It returns true if both arrays have the same length and all corresponding elements are equal according to their equals() method (for objects) or direct value comparison (for primitives).

// Demonstrates array comparison using Arrays.equals()

import java.util.Arrays;

class CompareArrays {
    public static void main(String[] args) {
        int[] arr1 = {1, 2, 3};
        int[] arr2 = {1, 2, 3};

        if (Arrays.equals(arr1, arr2)) {
            System.out.println("Same"); // This will be the output
        } else {
            System.out.println("Not same"); 
        }
    }
}

Output:

Same

Deep Comparison with Arrays.deepEquals()

Arrays.equals() performs a shallow comparison. For nested arrays (arrays of arrays), you need Arrays.deepEquals(). This method recursively compares the contents of nested arrays.

// Demonstrates deep array comparison using Arrays.deepEquals()

import java.util.Arrays;

class CompareArrays {
    public static void main(String[] args) {
        int[][] arr1 = {{1, 2}, {3, 4}};
        int[][] arr2 = {{1, 2}, {3, 4}};


        if (Arrays.deepEquals(arr1, arr2)) {
            System.out.println("Same"); // This will be the output
        } else {
            System.out.println("Not same");
        }
    }
}

Output:

Same

Arrays.deepEquals() handles multidimensional arrays and even cyclic object graphs (where an object refers back to itself) without falling into infinite loops. It ensures comprehensive equality checks for complex data structures.

Conclusion

Choosing the right method for array comparison is crucial for accurate results. While the “==” operator compares references, Arrays.equals() compares content for single-dimensional arrays. For nested arrays or multidimensional arrays, Arrays.deepEquals() provides a robust solution for deep comparison. Understanding these differences ensures you select the appropriate method for your specific comparison needs in Java.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *