deepEquals and equals are two static methods of the Arrays class in Java, but what is the difference 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)); // false System.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)); // true System.out.println(Arrays.deepEquals(name1, name2)); // true }}
Summary:
1. Use deepEquals For determining whether two specified arrays are deeply equal to each other, this method is suitable for nested arrays of any depth.
2. Equals is used to determine whether two arrays are equal. If the two arrays contain the same elements in the same order, it returns true, otherwise it returns false.
3. By comparing "Code 1" and "Code 2" we can draw the conclusion that if two arrays use equals to return true, then use deepEquals to also return true. That is to say, both arrays being compared are As long as it is a one-dimensional array, there is no difference between the comparison results of equals and deepEquals;
4. If you want to compare more arrays, you need to use the deepEquals method;
More Arrays in Java Two methods of the class: deepEquals and equals. For related articles, please pay attention to the PHP Chinese website!