【无标题】deep clone 相关问题

class中含有references variables的

protected 类型在 parent/subclass中的accessibility

Java:由Object.clone()而引出的protected权限问题_BAOLIANG196的博客-CSDN博客

 deep clone方法 -- override Object.clone():

 @Override
    public Object clone() throws CloneNotSupportedException{
       return (MyComplexType)super.clone();
 }

class中含有references variables的deep clone: 

import java.util.ArrayList;
import java.util.List;

public class deepCloneDemo {

    public static void main(String[] args) throws CloneNotSupportedException {
        System.out.println("=============================== shadow copy ======================");
        MySimpleType my = new MySimpleType(2,3);
        System.out.println(my);
        MySimpleType my2 = (MySimpleType) my.clone(); //since it's the primative type var, it get deep copied.
        System.out.println("my2: " + my2);

        my2.a = 100;
        System.out.println("now my2: " + my2);
        System.out.println("now my: " + my);
        System.out.println("===============================");
        System.out.println("=============================== deep clone without changes in /* */======================");
        MyComplexType mc = new MyComplexType(2,3);
        System.out.println(mc);
        MyComplexType mc2 = (MyComplexType) mc.clone();
        System.out.println("mc2:" + mc2);

        mc2.a = 10;
        mc2.b = 5;
        System.out.println("now only int[] in mc2 should be a deep copy: " + mc2);
        System.out.println("now int[] in mc should be no changes: " + mc);
        System.out.println("===============================");

        MyComplexType mc22 = (MyComplexType) mc.clone();
        int[] arr2 = new int[3];
        for(int i = 0; i < 3; i++){
            arr2[i] = 100;
        }
        mc22.arr = arr2;
        System.out.println("now mc22: " + mc22);

        mc22.list.get(0).set(0, 1); //since List<List> is a reference of reference, so the inner list is still  a shadow copy
        System.out.println("now list<list> in mc22 is still shadow clone: " + mc22);
        System.out.println("now list<list> in mc also changing: " + mc); //List<List> is still a shadow clone

        System.out.println("===============================deep clone with changes in /* */=================");

        MyComplexType mc3 = (MyComplexType) mc.clone(); //use deep clone
        mc3.list.get(0).set(0, 100);
        System.out.println("mc3:" + mc3);
        System.out.println("now mc: " + mc);

    }
}

class MySimpleType implements Cloneable{
    int a;
    int b;
    public MySimpleType(int x, int y){
        a = x;
        b = y;
    }

    @Override
    public String toString(){
        return "(" + a + ", " + b + ")";
    }

    @Override
    public Object clone() throws CloneNotSupportedException{
        MySimpleType newObject = (MySimpleType)super.clone();
        return newObject;
    }
}

class MyComplexType implements Cloneable{
    int a;
    int b;
    int[] arr;
    List<Integer> l;
    List<List<Integer>> list;

     public MyComplexType(int x, int y){
        a = x;
        b = y;
        arr = new int[a];
        for(int i = 0 ; i < a; i++){
            arr[i] = b;
        }
        l = new ArrayList<>();
        for(int i = 0 ; i < a; i++) {
            l.add(y);
        }
        list = new ArrayList<>();
        list.add(new ArrayList<>());
        list.get(0).add(a);
        list.get(0).add(b);
    }

    @Override
    public String toString(){
        return "(" + a + ", " + b + ", " + arr.toString()  + ", " + arr[0] + ", "+ arr[1] + "... "  + l.toString() + ", " + list.toString() + ")";
    }

    @Override
    public Object clone() throws CloneNotSupportedException{
        MyComplexType newObject = (MyComplexType)super.clone();

         //to ensure all components to be correctly cloned
        newObject.arr = (int[]) arr.clone();

        List<Integer> lcopy = new ArrayList<>();
        for(Integer n : l) {
            lcopy.add(n);
        }

        List<List<Integer>> listCopy = new ArrayList<>();
        for(List<Integer> line :list){
            List<Integer> lineCopy = new ArrayList<>();
            for(Integer node : line) {
                lineCopy.add(node);
            }
            listCopy.add(lineCopy);
        }
        newObject.list = listCopy;


        return newObject;
    }
}

不包含/**/中的output:

============== deep clone without changes in /* */======================
(2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
mc2:(2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
now only int[] in mc2 should be a deep copy: (10, 5, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
now int[] in mc should be no changes: (2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
===============================
now mc22: (2, 3, [I@13a57a3b, 100, 100... [3, 3], [[2, 3]])
now list<list> in mc22 is still shadow clone: (2, 3, [I@13a57a3b, 100, 100... [3, 3], [[1, 3]])
now list<list> in mc also changing: (2, 3, [I@3fb6a447, 3, 3... [3, 3], [[1, 3]])

 包含/**/中的部分的所有output:

=============================== shadow copy ======================
(2, 3)
my2: (2, 3)
now my2: (100, 3)
now my: (2, 3)
===============================
=============================== deep clone without changes in /* */======================
(2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
mc2:(2, 3, [I@13a57a3b, 3, 3... [3, 3], [[2, 3]])
now only int[] in mc2 should be a deep copy: (10, 5, [I@13a57a3b, 3, 3... [3, 3], [[2, 3]])
now int[] in mc should be no changes: (2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
===============================
now mc22: (2, 3, [I@7ca48474, 100, 100... [3, 3], [[2, 3]])
now list<list> in mc22 is still shadow clone: (2, 3, [I@7ca48474, 100, 100... [3, 3], [[1, 3]])
now list<list> in mc also changing: (2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])
===============================deep clone with changes in /* */=================
mc3:(2, 3, [I@337d0578, 3, 3... [3, 3], [[100, 3]])
now mc: (2, 3, [I@3fb6a447, 3, 3... [3, 3], [[2, 3]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值