1.数组中存放的是对象的地址。
public class ArrayTest3 {
public static void main(String[] args) {
I[] i = new I[2];
i[0] = new C();
i[1] = new C();
}
}
interface I{}
class C implements I{}
上面的代码是可以编译运行通过的,同样验证了上面的结论:数组中存放的是对象的地址。
无论生成什么样类型的数组,数组中存放的都是该类型对象的地址,而非该类型的对象。如果没有给数组赋予对象,则存放null。
上面的 I[] i = new I[2];代码并非生成I的对象,接口是无法生成对象的。i数组只能存放实现了I接口的类的对象地址。
2.数组也是对象,下面代码用 equals方法比较两个存放相同数字的数组对象是假,因为是两个不同的对象。
public static void main(String[] args) {
int[] a = {1,2,3};
int[] b = {1,2,3};
System.out.println(a.equals(b));
}
3.比较两个数组的内容是否相等:
public class ArrayTest5 {
public static boolean isEquals(int[] a,int[] b){
if(a == null || b == null){
return false;
}
if(a.length != b.length){
return false;
}
for(int i=0; i<a.length; i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] a = {1,2};
int[] b = {1,3};
System.out.println(isEquals(a,b));
}
}
转载于:https://blog.51cto.com/zlfwmm/1695833