比较两个相同类型的数组的元素

时间:2018-07-10 16:34:13

标签: java

 Student[] arr1 = { new Student(111, "bbbb", "london"), new 
 Student(131, "aaaa", "nyc"), new Student(121, "cccc", "jaipur") };

Student[] newArr = {  new Student(131, "aaaa", "nyc"), new 
Student(111, "bbbb", "london"), new Student(121, "cccc", "jaipur") };

使用链接中提供的相同equals方法比较这两个,为什么它们不相等?数组都仍然包含相同的元素。...

public boolean equals(Object obj) {         
        // typecast obj to Student so that we can compare students
        Student s = (Student) obj;

        return this.rollno == s.rollno && this.name.equals(s.name)
                                && this.address.equals(s.address);
}

这是链接here中提供的equals函数

1 个答案:

答案 0 :(得分:1)

您只需阅读文档即可获得答案。

Arrays::equals

  

如果两个指定的对象数组等于1,则返回true   另一个。如果两个数组都包含,则认为这两个数组相等   相同数量的元素,以及所有对应的元素对   在两个数组中相等。考虑两个对象e1和e2   等于(e1 == null?e2 == null:e1.equals(e2))。 换句话说,   如果两个数组在相同的位置包含相同的元素,则它们相等   订单。另外,如果两个数组引用均相等,则认为它们相等   空。

强调我。

相关问题