![]() |
VOOZH | about |
Arrays.deepEquals() method comes under the Arrays class in Java. It is used to check two arrays, whether single-dimensional or multi-dimensional, are equal or not. It compares arrays element by element, even when dealing with nested arrays.
Example:
Below is a simple example that uses Arrays.deepEquals() to check if two multidimensional arrays are equal.
arr1 equals with arr2: true arr2 equals with arr3: false
Table of Content
public static boolean deepEquals(Object[] o1, Object[] o2)
Parameter:
Return Type: It returns true, if the two arrays are deeply equal (all elements and nested arrays are equal), otherwise it will return false.
Arrays.deepEquals() with Multidimensional Arrays of IntegersIn this example, we will check if two-dimensional arrays of integers are equal at all levels using Arrays.deepEquals().
a1 is equal to a2: false a2 is equal to a3: false a1 is equal to a3: true
Arrays.deepEquals() with Arrays of User-Defined ObjectsIn this example, we will check if arrays of user-defined Employee objects are equal by overriding the equals() method to define the equality of different parameters in a user-defined class.
e1 is equal to e2: true e2 is equal to e3: false e1 is equal to e3: false