【发布时间】:2017-01-29 03:27:05
【问题描述】:
在这个例子中展示了如何做到这一点:
Student[] stud1 = new Student[2];
Student[] stud2 = new Student[2];
Student[] stud3 = new Student[2];
boolean b;
stud1[0]= new Student("Johnny","Bravo");
stud1[1]= new Student("Ace","Ventura");
stud2[0]= new Student("Ash","Ketchum");
stud2[1]= new Student("Mike","Wazowski");
b = Arrays.equals(stud1,stud2);
System.out.println(b);
stud2 [0] = stud1[0];
stud2[1] = stud1[1];
b = Arrays.equals(stud1,stud2);
System.out.println(b);
stud3 = stud1;
b = Arrays.equals(stud1,stud3);
System.out.println(b);
}
结果是假真真。 Arrays.equals 到底比较的是什么? 谢谢。
【问题讨论】:
-
什么是 Arrays.equals 的精确比较? 它检查元素之间的相等性,由
Object.equals()定义。你对某些特定的事情感到惊讶吗? -
这里的元素是对象;是比较参考 stud1[0] 与 stud2[0] 和 stud2[0] 与 stud2[1] 还是比较里面的内容(“Johnny”、“Bravo”与“Ash”、“Ketchum”....)
-
它正在使用
Object.equals()。如果您覆盖了该方法,它将使用您的实现。否则,它会检查身份或引用相等性。