Java Arrays deepEquals() Method
Example
Compare two multidimensional arrays with equals() and with deepEquals():
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[][] myNumbers = {{1, 2, 3, 4}, {5, 6, 7}};
int[][] myNumbers2 = {{1, 2, 3, 4}, {5, 6, 7}};
System.out.println(Arrays.equals(myNumbers, myNumbers2));
System.out.println(Arrays.deepEquals(myNumbers, myNumbers2));
}
}
Definition and Usage
The deepEquals() method checks if two arrays are equal. It also compares the arrays inside them, so it works with multidimensional arrays (arrays of arrays).
The equals() method only checks if the inner arrays are the same objects. It does not compare their items. That is why it returns false in the example.
Syntax
public static boolean deepEquals(Object[] array1, Object[] array2)
Parameter Values
| Parameter | Description |
|---|---|
| array1 | Required. The first array to compare. |
| array2 | Required. The second array to compare. |
Technical Details
| Returns: | true if the arrays and all the arrays inside them are equal, otherwise false. |
|---|
Related Pages
❮ Arrays Methods