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
import java.util.*;
class GFG {
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" );
}
}
|
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
import java.util.Arrays;
class GFG {
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" );
else
System.out.println( "Not 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
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
int inarr1[] = { 1 , 2 , 3 };
int inarr2[] = { 1 , 2 , 3 };
Object[] arr1 = { inarr1 };
Object[] arr2 = { inarr2 };
if (Arrays.equals(arr1, arr2))
System.out.println( "Same" );
else
System.out.println( "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
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
int inarr1[] = { 1 , 2 , 3 };
int inarr2[] = { 1 , 2 , 3 };
Object[] arr1 = { inarr1 };
Object[] arr2 = { inarr2 };
if (Arrays.deepEquals(arr1, arr2))
System.out.println( "Same" );
else
System.out.println( "Not 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
import java.util.Arrays;
class GFG {
public static void main(String[] args)
{
int inarr1[] = { 1 , 2 , 3 };
int inarr2[] = { 1 , 2 , 3 };
Object[] arr1 = { inarr1 };
Object[] arr2 = { inarr2 };
Object[] outarr1 = { arr1 };
Object[] outarr2 = { arr2 };
if (Arrays.deepEquals(outarr1, outarr2))
System.out.println( "Same" );
else
System.out.println( "Not same" );
}
}
|
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.