关于两个数组比较问题:代码如下,在Student类中自己写一个equals方法,参数是Student类型的,为什么在调用Arrays.equals方法的时候,这个自己定义的方法得不到调用呢?一定要覆盖掉Object基类中的equals方法才行么?
package ArrayTest;
import java.util.*;public class Test {
public static void main(String[] args){
Student[] stu1 = new Student[]{
new Student(1),new Student(2),new Student(3)
};
Student[] stu2 = new Student[]{
new Student(1),new Student(2),new Student(3)
};
System.out.println(Arrays.equals(stu1, stu2));
}
}class Student{
private int age ;
public Student(int age){
this.age = age;
}
public boolean equals(Student s){
return /*((Student)*/s.age==this.age;
}
}
package ArrayTest;
import java.util.*;public class Test {
public static void main(String[] args){
Student[] stu1 = new Student[]{
new Student(1),new Student(2),new Student(3)
};
Student[] stu2 = new Student[]{
new Student(1),new Student(2),new Student(3)
};
System.out.println(Arrays.equals(stu1, stu2));
}
}class Student{
private int age ;
public Student(int age){
this.age = age;
}
public boolean equals(Student s){
return /*((Student)*/s.age==this.age;
}
}
if (o.getClass() != Student.class)
return false;
Student s = (Student) o;
return s.age == this.age;
}
if (a==a2)
return true;
if (a==null || a2==null)
return false; int length = a.length;
if (a2.length != length)
return false; for (int i=0; i<length; i++) {
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1==null ? o2==null : o1.equals(o2)))
return false;
} return true;
}