Java Program to Check if two Arrays are Equal or not (original) (raw)
Last Updated : 25 Nov, 2024
In Java, **comparing two arrays means checking if they have the same length and identical elements. This checking process ensures that both arrays are equivalent in terms of content and structure.
**Example:
In Java, the simplest way to check if two arrays are equal in Java is by using the built-in Arrays.equals() method.
Java `
import java.util.Arrays;
public class GFG { public static void main(String[] args) {
int a[] = {10, 20, 30};
int b[] = {10, 20, 30};
// Using Arrays.equals() to check
// if two arrays are equal
boolean r = Arrays.equals(a, b);
if (r) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
}
}
`
**Note: If there are repetitions, then counts of repeated elements should be the same for two arrays to be equal.
Other Methods to Check Arrays are Equal or Not
**Using Arrays.deepEquals() method
In this example, we will use Arrays.deepEquals() to compare two nested arrays, including multi-dimensional elements. The method checks complete content along with structural equality of both arrays.
Java `
import java.util.Arrays;
public class ArraysDeepEquals {
// Function to check if nested arrays are equal
private static void check(Integer[][] a, Integer[][] b) {
// Using Arrays.deepEquals() to check if two nested arrays are equal
boolean res = Arrays.deepEquals(a, b);
// Printing the result directly
if (res) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
}
public static void main(String[] args) {
Integer[][] arr1 = { {10, 20, 30}, {40, 50} };
Integer[][] arr2 = { {10, 20, 30}, {40, 50} };
check(arr1, arr2);
Integer[][] arr3 = { {10, 20, 30}, {40, 50} };
Integer[][] arr4 = { {30, 25, 40}, {50, 61} };
check(arr3, arr4);
}
}
`
**Explanation: First we initialize two nested arrays and insert elements in each array. Then **Arrays.deepEquals() method is called to check if the nested arrays are equal with the result stored in a boolean variable **res. At last, the result is printed and indicates whether the arrays are “Equal” or “Not Equal.”
**Without Using pre-defined Function
In this example, we check manually two arrays, if they are equal or not. We check it by comparing their lengths and individual elements. A loop iterates through each element of both arrays and if all elements match, the arrays are considered “Equal” otherwise, they are “Not Equal”.
Java `
import java.util.Arrays;
public class ArrayEquals {
// Function to check if arrays are equal
private static void check(int a[], int b[]) {
// Initialize result to true
boolean res = true;
// Check if length of the two arrays are equal
if (a.length == b.length) {
// Loop through elements to compare them
for (int i = 0; i < a.length; i++) {
if (a[i] != b[i]) {
// Set false if any element differs
res = false;
break;
}
}
} else {
// Set false if lengths differ
res = false;
}
if (res) {
System.out.println("Equal");
} else {
System.out.println("Not Equal");
}
}
public static void main(String[] args) {
int a[] = { 10, 20, 30 };
int b[] = { 10, 20, 30 };
check(a, b);
int c[] = { 10, 20, 30 };
int d[] = { 45, 50, 55, 60, 65 };
check(c, d);
}
}
`
When to Use Which Method
- **Using
Arrays.equals()
: When we need to compare one-dimensional arrays, we should use this method as it checks if both arrays have the same elements in the same order. - **Using
Arrays.deepEquals()
: When we need to compare multi-dimensional or nested arrays, we can use this method as it checks structural and element equality across all nested levels. - **Without Pre-defined Methods: When we need custom comparison logic or we want to avoid library methods, we can use a manual loop comparison.