Object 的equals方法,最后一段可以解决你的问题吧--------------------------------------------------------------- equalspublic boolean equals(Object obj)Indicates whether some other object is "equal to" this one. The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).Parameters:obj - the reference object with which to compare.Returns:true if this object is the same as the obj argument; false otherwise.
或者这样 1、生成ArrayTest1的实例a,通过a来调用equals public class ArrayTest1 { public boolean equals(Object[] obj1,Object[] obj2){ System.out.println("123"); return true; } public static void main(String[] args){ String[] a1=new String[]{new String("1st"), new String("2st"), new String("3st")}; //String[] a2=new String[5]; //a2=a1; String[] a2=new String[]{new String("1st"), new String("2st"), new String("3st")}; // System.out.println("the length of a1 is :"+a2.length); ArrayTest1 a=new ArrayTest1();
} }2、将equals定义为静态的,然后直接调用 public class ArrayTest1 { public static boolean equals(Object[] obj1,Object[] obj2){ System.out.println("123"); return true; } public static void main(String[] args){ String[] a1=new String[]{new String("1st"), new String("2st"), new String("3st")}; //String[] a2=new String[5]; //a2=a1; String[] a2=new String[]{new String("1st"), new String("2st"), new String("3st")}; // System.out.println("the length of a1 is :"+a2.length);
equalspublic boolean equals(Object obj)Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation: It is reflexive: for any reference value x, x.equals(x) should return true. It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified. For any non-null reference value x, x.equals(null) should return false. The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).Parameters:obj - the reference object with which to compare.Returns:true if this object is the same as the obj argument; false otherwise.
return true;
}这段代码好象没起到作用?没有覆盖原有的equals()?
a1==a2 比较的指针当然不同,
a1.equals(a2)) 比较的似内容, 自定义的对象么重写equals方法. String[] a3=new String[]{new String("1st"),
new String("2st"),
new String("3st")};
String[] a4 = a3;
System.out.println("a3=a4 is"+(a3==a4));
System.out.println("a3 equals a4 is"+(a3.equals(a4))); 这方返回的都是TRUE;2)复合对象比较时需重写equals方法,
根据不同情况,
对于数组的话,
可以用楼上上的.
但你的equals方法是属于
类
ArrayTest1
的,所以
a1.equals(a2)方法调用的还是Object的equals方法
不会调用你的方法
所以不起作用,你可以将你写的equals方法改为equals1
你会发现a1中不会有equals1方法的如果:
ArrayTest1 a=new ArrayTest1()
ArrayTest1 b=new ArrayTest1()
则可以调用你的equals方法:
即a.equals(b)=true
1、生成ArrayTest1的实例a,通过a来调用equals
public class ArrayTest1 {
public boolean equals(Object[] obj1,Object[] obj2){
System.out.println("123");
return true;
}
public static void main(String[] args){
String[] a1=new String[]{new String("1st"),
new String("2st"),
new String("3st")};
//String[] a2=new String[5];
//a2=a1;
String[] a2=new String[]{new String("1st"),
new String("2st"),
new String("3st")};
// System.out.println("the length of a1 is :"+a2.length);
ArrayTest1 a=new ArrayTest1();
System.out.println("a1=a2 is"+(a1==a2));
System.out.println("a1 equals a2 is"+(a.equals(a1,a2)));
}
}2、将equals定义为静态的,然后直接调用
public class ArrayTest1 {
public static boolean equals(Object[] obj1,Object[] obj2){
System.out.println("123");
return true;
}
public static void main(String[] args){
String[] a1=new String[]{new String("1st"),
new String("2st"),
new String("3st")};
//String[] a2=new String[5];
//a2=a1;
String[] a2=new String[]{new String("1st"),
new String("2st"),
new String("3st")};
// System.out.println("the length of a1 is :"+a2.length);
System.out.println("a1=a2 is"+(a1==a2));
System.out.println("a1 equals a2 is"+(equals(a1,a2)));
}
}