qq_30951345 2016-05-03 05:36 采纳率: 0%
浏览 1264
已结题

数组复制问题--Interger类型数组

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类型的数组在此例中应该是浅复制啊?怎么结果是不符合浅复制,而是深复制

  • 写回答

2条回答

  • little_how 2016-05-03 05:52
    关注

    Integer只是int类型的装箱,和其他引用类型不同。
    你改了下标的数字,就改了,而不是改了引用的值。

    而且以你的这种方式,就算是其他引用类型,值也会只改变数组2,
    因为你的下标指向了一个新地址,代表指针与数组一的原指向地址已经不同了。

    和深复制浅复制没关系,浅复制表现在,复制过程中如果有引用对象,那么,复制的是该引用的地址,而不是对象,所以当在复制后的对象上改变其
    引用地址所指向的对象值的时候会影响复制前的值。

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable