龙空技术网

深拷贝、浅拷贝与赋值的区别

王朋code 45

前言:

而今我们对“list赋值给另一个list深拷贝”大致比较珍视,你们都需要分析一些“list赋值给另一个list深拷贝”的相关文章。那么小编同时在网摘上网罗了一些有关“list赋值给另一个list深拷贝””的相关文章,希望大家能喜欢,看官们快快来了解一下吧!

赋值

只是改变了对象的引用,两个引用指向的依然是同一个对象,所以当其中一个引用改变了对象,另一个引用指向的依然是这个被改变的对象

//创建一个对象ToApprListVO toApprListVO = new ToApprListVO();toApprListVO.setFirstNode("1");NodeAttr nodeAttr = new NodeAttr();nodeAttr.setNodename("aa");toApprListVO.setNodeAttr(nodeAttr);//赋值ToApprListVO toApprListVO1=toApprListVO;toApprListVO.setFirstNode("2");nodeAttr.setNodename("bb");System.out.println("原始对象:"+toApprListVO);System.out.println("赋值对象:"+toApprListVO1);//结果:是一致的,说明两者指向的是同一个对象原始对象:ToApprListVO{firstNode='2', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}赋值对象:ToApprListVO{firstNode='2', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}
浅拷贝

如果一个类中有基本类型变量和引用类型变量。创建一个该类的对象,使用浅拷贝拷贝这个对象生成一个新的对象时,修改原来对象的基本类型和引用类型的值,新生成的对象的基本类型并不会被修改,但是引用类型会被修改。原因就是浅拷贝拷贝对象时,对象里的引用变量拷贝的只是引用地址,并没有拷贝对象。

//我们使用Cloneable接口中的clone方法进行前拷贝public class ToApprListVO implements Cloneable{    /**     * 首节点名称     */    private String firstNode;  	//此时当前对象并没有实现Cloneable接口    private NodeAttr nodeAttr;    //object中的方法,必须实现Cloneable接口    @Override    public Object clone() throws CloneNotSupportedException {        ToApprListVO toApprListVO = (ToApprListVO)super.clone();        return toApprListVO;    }		...}
//创建一个对象ToApprListVO toApprListVO = new ToApprListVO();toApprListVO.setFirstNode("1");NodeAttr nodeAttr = new NodeAttr();nodeAttr.setNodename("aa");toApprListVO.setNodeAttr(nodeAttr);//拷贝ToApprListVO toApprListVO2= (ToApprListVO)toApprListVO.clone();toApprListVO.setFirstNode("2");nodeAttr.setNodename("bb");System.out.println("原始对象:"+toApprListVO);System.out.println("浅拷贝对象:"+toApprListVO2);//结果:基本类型没有改变,但是引用类型被改变了,说明两者是同一个对象原始对象:ToApprListVO{firstNode='2', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}赋值对象:ToApprListVO{firstNode='2', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}浅拷贝对象:ToApprListVO{firstNode='1', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}
深拷贝

深拷贝会拷贝所有的属性,并拷贝属性指向的动态分配的内存。当对象和它所引用的对象一起拷贝时即发生深拷贝。深拷贝相比于浅拷贝速度较慢并且花销较大。

public class NodeAttr implements Cloneable{    private String nodename;    private String nodetype;    private String rolename;    private String assigner;    private String reserve;    private String company;    private String department;		...    @Override    public Object clone() throws CloneNotSupportedException {        return super.clone();    }}
public class ToApprListVO implements Cloneable{    /**     * 首节点名称     */    private String firstNode;    private PageInfo<StaffInfoVo> pageInfo;    private NodeAttr nodeAttr;    @Override    public Object clone() throws CloneNotSupportedException {        ToApprListVO toApprListVO = (ToApprListVO)super.clone();      	//这里也调用了nodeAttr的clone方法        toApprListVO.nodeAttr = (NodeAttr)nodeAttr.clone();        return toApprListVO;    }   	...}
//原始对象ToApprListVO toApprListVO = new ToApprListVO();toApprListVO.setFirstNode("1");NodeAttr nodeAttr = new NodeAttr();nodeAttr.setNodename("aa");toApprListVO.setNodeAttr(nodeAttr);//深拷贝对象ToApprListVO toApprListVO2= (ToApprListVO)toApprListVO.clone();toApprListVO.setFirstNode("2");nodeAttr.setNodename("bb");System.out.println("原始对象:"+toApprListVO);System.out.println("深拷贝对象:"+toApprListVO2);//结果:基本类型和引用类型都不会改变原始对象:ToApprListVO{firstNode='2', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}赋值对象:ToApprListVO{firstNode='2', pageInfo=null, nodeAttr=NodeAttr{nodename='bb', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}深拷贝对象:ToApprListVO{firstNode='1', pageInfo=null, nodeAttr=NodeAttr{nodename='aa', nodetype='null', rolename='null', assigner='null', reserve='null', company='null', department='null'}}

标签: #list赋值给另一个list深拷贝