Check if two integer arrays are equal in Java
This post will check if two integer arrays are equal in Java.
Two integer arrays are considered equal if both arrays contain the same number of elements and contain the same elements in the same order. To compare two integer arrays in Java, we can use one of the following methods:
1. Using Arrays.equals() and Arrays.deepEquals() method
We can also take advantage of two convenient methods provided by java.util.Arrays class for array comparison: equals() and deepEquals(). Both these methods are overloaded to accept all primitive types and an array of objects. The Arrays.equals() method is a static method that takes two integer arrays as parameters and returns a boolean value that indicates if they are equal or not. Two arrays are considered equal if they have the same length and the same elements in the same order. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[] first = { 1, 2, 3 }; int[] second = { 4, 5, 6 }; boolean result = Arrays.equals(first, second); if (result) { System.out.println("Both arrays are equal"); } else { System.out.println("Both arrays are not equal"); } } } |
Output:
Both arrays are not equal
For nested arrays, the Arrays.equals() method won’t work. We need to use the Arrays.deepEquals() method, which is similar to the equals() method, but it can also handle multi-dimensional arrays. It performs a deep comparison of the elements of the arrays, using any custom equals methods they may have. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; class Main { public static void main(String[] args) { int[][] first = { {1, 2, 3}, {4, 5, 6} }; int[][] second = { {1, 2, 3}, {4, 5, 6} }; System.out.println("Arrays.equals() returns " + Arrays.equals(first, second)); System.out.println("Arrays.deepEquals() returns " + Arrays.deepEquals(first, second)); } } |
Output:
Arrays.equals() returns false
Arrays.deepEquals() returns true
2. Using for loop
We can also manually compare two integer arrays by iterating through their elements and checking if they are equal or not. We need to first compare the lengths of the arrays and then use a for loop to compare each element. For example, we can compare single-dimensional integer arrays like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
import java.util.stream.IntStream; class Main { public static boolean isEqual(int[] first, int[] second) { // base case 1 if (first == second) { return true; } // base case 2 if (first == null || second == null || first.length != second.length) { return false; } // loop through the elements and compare elements at the same index for (int i = 0; i < first.length; i++) { if (first[i] != second[i]) { return false; } } return true; // The for loop can be replaced with below code which uses the Stream API // return IntStream.range(0, first.length).noneMatch(i -> first[i] != second[i]); } public static void main(String[] args) { int[] first = { 1, 2, 3 }; int[] second = { 1, 2, 3 }; boolean result = isEqual(first, second); if (result) { System.out.println("Both arrays are equal"); } else { System.out.println("Both arrays are not equal"); } } } |
Output:
Both arrays are equal
Similar to the single-dimensional arrays, we can write our own method for checking array equality for two-dimensional arrays. For example, we can use something like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
import java.util.Arrays; class Main { public static boolean isEqual(int[][] first, int[][] second) { // base case 1 if (first == second) { return true; } // base case 2 if (first == null || second == null || first.length != second.length) { return false; } // loop through each sub-array for (int i = 0; i < first.length; i++) { // check if both sub-arrays has the same length if (first[i].length != second[i].length) { return false; } // loop through the sub-array and compare elements at the same index for (int j = 0; j < first[i].length; j++) { if (first[i][j] != second[i][j]) { return false; } } } return true; } public static void main(String[] args) { int[][] first = { {1, 2, 3}, {4, 5, 6} }; int[][] second = { {1, 2, 3}, {4, 5, 6} }; boolean result = isEqual(first, second); if (result) { System.out.println("Both arrays are equal"); } else { System.out.println("Both arrays are not equal"); } } } |
Output:
Both arrays are equal
That’s all about checking if two integer arrays are equal in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)