Two Methods of the Arrays class in Java: deepEquals and equals
DeepEquals and equals are two static methods of the Arrays class in Java, but what are the differences between them?
Code 1,
import java.util.Arrays;public class Test {public static void main(String[] args) {String[][] name1 = {{ "G","a","o" },{ "H","u","a","n"},{ "j","i","e"}};String[][] name2 = {{ "G","a","o" },{ "H","u","a","n"},{ "j","i","e"}};System.out.println(Arrays.equals(name1, name2)); // falseSystem.out.println(Arrays.deepEquals(name1, name2));// true}}
Code 2,
import java.util.Arrays;public class Test {public static void main(String[] args) {String[] name1 = {"G","a","o","H","u","a","n","j","i","e"};String[] name2 = {"G","a","o","H","u","a","n","j","i","e"};System.out.println(Arrays.equals(name1, name2)); // trueSystem.out.println(Arrays.deepEquals(name1, name2)); // true}}
Summary:
1. deepEquals is used to determine whether two specified arrays are deep equal to each other. This method applies to nested arrays of any depth.
2. equals is used to determine whether two arrays are equal. If the two arrays contain identical elements in the same order, true is returned. Otherwise, false is returned.
3. By comparing "code 1" and "Code 2", we can conclude that if the two arrays use equals to return true, deepEquals also returns true, that is to say, the comparison results of equals and deepEquals are no different on the premise that both arrays are one-dimensional arrays;
4. If you want to compare multiple objects to an array, you must use the deepEquals method;