public class TestArray {
public static void main(String[] args) {
TestArray ta = new TestArray();
//关于数组的测试
int[] a = {1,2,3};
int[] b = a;
System.out.println(a == b);//结果为true
b[2] = 5;
System.out.println(a == b);//结果为true
int[] c = new int[] {1,2,5};
int[] d = c;
System.out.println(a == c);//结果为false
//System.out.println(a.equals(c));//结果为false
System.out.println(Arrays.equals(a, c));//结果为true
System.out.println(c == d);//结果为true
System.out.println("*********");
System.out.println(ta.equals(a, c));//结果为true
}
/**重写equals方法*/
public boolean equals(int[] array1, int[] array2) {
boolean isNull = array1 == null || array2 == null;
if (isNull) {
return false;
} else {
boolean isLenEqual = array1.length == array2.length;
if (!isLenEqual) {
return false;
} else {
for (int i = 0; i < array1.length; i++) {
boolean isNumEqual = array1[i] == array2[i];
if (isNumEqual) {
return true;
} else {
return false;
}
}
}
}
return false;
}
}
关于数组间的==,equals和Arrays.equals的测试
最新推荐文章于 2024-08-14 22:10:41 发布