Comparing two arrays in Java means checking whether both arrays contain the same elements in the same order. For arrays to be equal, they must have the same length and corresponding elements must be equal.
- Array comparison is order-sensitive.
- Repeated elements must occur at the corresponding positions for Arrays.equals() to return
true
Example:
Input: arr1 = [10, 20, 30], arr2 = [10, 20, 30]
Output: EqualInput: arr1 = [10, 20, 30], arr2 = [30, 20, 10]
Output: Not Equal
Different Approaches to Check if two Arrays are Equal or not
1. Using Arrays.equals() Method
The Arrays.equals() method is the simplest way to compare two one-dimensional arrays. It checks whether both arrays have the same length and equal elements at corresponding positions.
import java.util.Arrays;
public class GFG {
public static void main(String[] args) {
int[] a = {10, 20, 30};
int[] b = {10, 20, 30};
// Compare both arrays
boolean result = Arrays.equals(a, b);
System.out.println(result ? "Equal" : "Not Equal");
}
}
Output
Equal
Explanation
- Two integer arrays a and b are created.
- Arrays.equals() compares their lengths and corresponding elements.
- Both arrays contain the same elements in the same order.
- Therefore, the method returns
true.
2. Using Arrays.deepEquals() Method
The Arrays.deepEquals() method is used to compare nested arrays, such as two-dimensional arrays. It recursively compares the elements of nested arrays.
import java.util.Arrays;
public class GFG {
public static void main(String[] args) {
Integer[][] arr1 = {
{10, 20, 30},
{40, 50}
};
Integer[][] arr2 = {
{10, 20, 30},
{40, 50}
};
// Compare nested arrays
boolean result = Arrays.deepEquals(arr1, arr2);
System.out.println(result ? "Equal" : "Not Equal");
}
}
Output
Equal
Explanation:
- Two two-dimensional arrays, arr1 and arr2, are created.
- Arrays.deepEquals() compares the outer arrays and their nested arrays.
- Corresponding elements are equal and have the same structure.
- Therefore, the method returns
true.
3. Without Using Predefined Methods
We can also compare two arrays manually without using Arrays.equals(). First, compare their lengths. If the lengths are equal, compare each corresponding element using a loop.
public class GFG {
public static boolean areEqual(int[] a, int[] b) {
// Check if lengths are different
if (a.length != b.length) {
return false;
}
// Compare corresponding elements
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] a = {10, 20, 30};
int[] b = {10, 20, 30};
System.out.println(areEqual(a, b) ? "Equal" : "Not Equal");
}
}
Output
Equal
Explanation:
- The areEqual() method first checks whether both arrays have the same length.
- If their lengths are different, it immediately returns false.
- If the lengths are equal, the for loop compares corresponding elements.
- If any pair of elements is different, the method returns false.
- If all elements match, the method returns true.
4. Comparing Arrays with Different Elements
If the arrays have different elements or the same elements in a different order, they are not considered equal using Arrays.equals().
import java.util.Arrays;
public class GFG {
public static void main(String[] args) {
int[] a = {10, 20, 30};
int[] b = {30, 20, 10};
System.out.println(Arrays.equals(a, b)
? "Equal"
: "Not Equal");
}
}
Output
Not Equal
Explanation: Although both arrays contain 10, 20, and 30, their elements are arranged in different orders. Arrays.equals() performs an order-sensitive comparison, so the arrays are not equal.