集合丶对象的深度复制

一丶对象复制

复制对象基本只要两步:

  1. 实现Cloneable接口
  2. 实现clone()方法,并调用父类clone()

需要注意,Objectclone()方法,具有开销小,速度快的特点。而且,原始的Object方法是被protected修饰的,在这里需要修改为public,如果不这么做,浅复制时没有问题,深复制就会遇到权限不够的问题。java继承还有个原则,就是子类覆写父类方法,访问修饰符权限不能低于父类。

代码:

@Data
public class Demo implements Cloneable{
    private int  demoValue;
    private String type;
    public Demo(){
    }
    public Demo(int demoValue){
        this.demoValue = demoValue;
    }
    @Override
    public Object clone() {
        Demo stu = null;
        try{
            stu = (Demo)super.clone();
        }catch(CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return stu;
    }
}

执行复制

public static void main(String[] args) {
    Demo d1 = new Demo(1);
    Demo d2 = d1;
    //浅复制
    d2.setDemoValue(2);
    //深复制
    Demo d3 = (Demo)d1.clone();
    d3.setDemoValue(3);
    System.out.println("d1的地址:"+d1);
    System.out.println("d2的地址:"+d2);
    System.out.println("d3的地址:"+d3);
}

执行结果

 二丶List集合深度复制

List<基本数据类型> 复制比较简单 就不在这里介绍了

现在介绍的是List<Object>类型的   数据复制

代码如下

List<Demo> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
    list.add(new Demo(i));
}
List<Demo> newList = new ArrayList<>();
List<Demo> list1 = new ArrayList<>();
//浅复制
list1 = list;

//深度复制
CollectionUtils.addAll(newList, new Object[list.size()]);
Collections.copy(newList, list);


list1.set(0,new Demo(5));
newList.set(0,new Demo(10));
System.out.println("原list值:" + list);
System.out.println("原list1值:" + list1);
System.out.println("新list值:" + newList);

 执行结果

 =======================================华丽的分割线=========================================

还有一种情况比较特殊的复制方式:

重新定义对象,不集成toString方法,

public class Demo implements Cloneable{

    public int getDemoValue() {
        return demoValue;
    }
    public void setDemoValue(int demoValue) {
        this.demoValue = demoValue;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
    private int demoValue;
    private String type;
    public Demo(){
    }
    public Demo(int demoValue){
        this.demoValue = demoValue;
    }
    @Override
    public Object clone() {
        Demo stu = null;
        try{
            stu = (Demo)super.clone();
        }catch(CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return stu;
    }

}

 执行代码

List<Demo> list = new ArrayList<>();
for (int i = 0; i < 3; i++) {
    list.add(new Demo(i));
}

//浅复制
List<Demo> list1 = new ArrayList<>();
//深度复制
CollectionUtils.addAll(list1, new Object[list.size()]);
Collections.copy(list1, list);

//list深度拷贝
List<Demo> newList = new ArrayList<>();
for(Demo d : list){
    Demo de = (Demo)d.clone();
    newList.add(de);
}

System.out.println("list地址:" + list);
System.out.println("list1地址:" + list);
System.out.println("newList地址:" + newList);

执行结果

 

从结果上  我们可以看得出来 list深度复制只能改变list自己的本质,并不能改变list所存储的对象值.

想要彻底改变  我们需要进行对象深度复制.这样我们才能从本质上,去改变list的输出物. 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值