deepEquals和equals是Java中Arrays類別的兩個靜態方法,可是它們之間有什麼樣的區別呢?
代碼一、
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 }}
代碼二、
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 }}
總結:
rrreee
用1於判定兩個指定數組彼此是否深層相等,此方法適用於任意深度的嵌套數組。
2、equals用於判定兩個數組是否相等,如果兩個數組以相同順序包含相同元素,則傳回true,否則傳回false。
3、透過比對「代碼一」和「代碼二」我們可以得出這樣一個結論:如果兩個數組使用equals返回true,則使用deepEquals也傳回true,也就是說在比較的兩個數組均在一維數組的前提下,equals和deepEquals的比較結果沒有差別;
4、如果要比較多為數組,則需要使用deepEquals方法;