栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【无标题】deep clone 相关问题

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【无标题】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 is a reference of reference, so the inner list is still  a shadow copy
        System.out.println("now list in mc22 is still shadow clone: " + mc22);
        System.out.println("now list in mc also changing: " + mc); //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 l;
    List> 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 lcopy = new ArrayList<>();
        for(Integer n : l) {
            lcopy.add(n);
        }

        List> listCopy = new ArrayList<>();
        for(List line :list){
            List 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 in mc22 is still shadow clone: (2, 3, [I@13a57a3b, 100, 100... [3, 3], [[1, 3]])
now 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 in mc22 is still shadow clone: (2, 3, [I@7ca48474, 100, 100... [3, 3], [[1, 3]])
now 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]])

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/704350.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号