先上两个例子
例子1:
public class IntegerArray{
public static void main(String[] args){
Integer[] scores1 = new Integer[]{1,2,3,4,5};
Integer[] scores2 = new Integer[scores1.length];
for(int i = 0; i < scores1.length; i++){
scores2[i] = scores1[i];
}
for(Integer score2: scores2){
System.out.print(score2);
}
System.out.println();
scores2[0] = 7;
for(Integer score1 : scores1){
System.out.print(score1);
}
System.out.println();
for(Integer score2: scores2){
System.out.print(score2);
}
}
}
--------------------------------------------------------
运行结果:
12345
12345
72345
--------------------------------------------------------
例子2:
public class ClassArray {
public static void main(String[] args){
Clothes[] c1 = {new Clothes("red", 'L'), new Clothes("blue", 'M')};
Clothes[] c2 = new Clothes[c1.length];
for(int i = 0; i < c1.length; i++){
c2[i] = c1[i];
}
for(Clothes c : c2){
System.out.print(c.color + "--" + c.size +" ");
}
c2[0].color = "black";
System.out.println();
for(Clothes c : c1){
System.out.print(c.color + "--" + c.size + " ");
}
System.out.println();
for(Clothes c : c2){
System.out.print(c.color + "--" + c.size +" ");
}
}
}
----------------------------------------------------------------------
运行结果:
red--L blue--M
black--L blue--M
black--L blue--M
----------------------------------------------------------------------
疑问:同样是类类型数组,为什么运行的结果不同,一个是2个数组变量分别指向2个不同的数组对象,
另一个则是2个数组变量指向同一个数组对象;难道就是因为Integer这个类比较特殊?希望知道的人帮忙解答一下。。。Corbie亚东
慕工程5894045
慕工程5894045
这老头坏的很_2019
相关分类