zoukankan      html  css  js  c++  java
  • Java 深拷贝与浅拷贝

    1.常量
         int aa=1;
    int bb=aa;
    aa=2;
    System.out.println(aa);//2
    System.out.println(bb);//1 常量池中值得变化

    2.数组
    浅拷贝:
    String a[]=new String[3];
    a[0]="1";
    a[1]="2";
    a[2]="3";
    for(int i =0;i<a.length;i++){
    System.out.println(a[i]);
    }
    System.out.println("---------------------------------");
    String b[]=a;
    b[2]="1";
    for(int i =0;i<a.length;i++){
    System.out.println(a[i]);
    }
    for(int i =0;i<b.length;i++){
    System.out.println(b[i]);
    }
    外加
    String d[]=new String[3];
    d=a;
    d[2]="1";结果都是一样的,都是都一块地址

    输出结果:

    1
    2
    3
    ---------------------------------
    1
    2
    1
    ---------------------------------
    1
    2
    1

      深拷贝,改变c的值,a的值不变

    String c[]=a.clone();
    c[2]="4";
    System.out.println("---------------------------------");
    for(int i =0;i<c.length;i++){
    System.out.println(c[i]);
    }
    System.out.println("---------------------------------");
    for(int i =0;i<a.length;i++){
    System.out.println(a[i]);
    }
    ---------------------------------
    1
    2
    4
    ---------------------------------
    1
    2
    3
  • 相关阅读:
    Root of AVL Tree
    04-树4 是否同一棵二叉搜索树
    03-树3 Tree Traversals Again
    03-树2 List Leaves
    283. Move Zeroes
    506. Relative Ranks
    492. Construct the Rectangle
    476. Number Complement
    461. Hamming Distance
    389. Find the Difference
  • 原文地址:https://www.cnblogs.com/foreverstudy/p/10642450.html
Copyright © 2011-2022 走看看