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
Integer只是int类型的装箱,和其他引用类型不同。
你改了下标的数字,就改了,而不是改了引用的值。
而且以你的这种方式,就算是其他引用类型,值也会只改变数组2,
因为你的下标指向了一个新地址,代表指针与数组一的原指向地址已经不同了。
和深复制浅复制没关系,浅复制表现在,复制过程中如果有引用对象,那么,复制的是该引用的地址,而不是对象,所以当在复制后的对象上改变其
引用地址所指向的对象值的时候会影响复制前的值。
你可以试一下scores2=scores1,应该就是你要的浅复制
你逐个赋的相同的值,当然会是一样的