ArrayList的深拷贝与浅拷贝

本文介绍了Java中四种实现浅拷贝的方法及一种实现深拷贝的方法,并通过具体示例展示了不同拷贝方式下对象变化的影响。

浅拷贝:被复制对象的任何变量都含有和原来的对象相同的值,而任何的对其他对象的引用仍然指向原来的对象。对拷贝后的引用的修改,还能影响原来的对象。 

深拷贝:把要复制的对象所引用的对象都复制了一遍,对现在对象的修改不会影响原有的对象。


介绍四种浅拷贝与一种深拷贝。

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

/**
 *4种浅层拷贝
 *1种深层拷贝
 */
public class Test1 {

    /**
     * 浅层copy1
     */
    public void copy1(ArrayList<Person> oldList){        
        ArrayList<Person> newList = new ArrayList<Person>();
        newList=(ArrayList<Person>) oldList.clone();
       System.out.println("oldList:"+oldList);
       System.out.println("newList:"+newList);
       oldList.remove(0);
       System.out.println("oldList:"+oldList);
       System.out.println("newList:"+newList);
       oldList.get(0).name="WYM";
       System.out.println("oldList:"+oldList);
       System.out.println("newList:"+newList);
    }
    /**
     * 浅层copy2
     */
    public void copy2(ArrayList<Person> oldList){
        ArrayList<Person> newList = new ArrayList<Person>();
        for(Person o :oldList){
            newList.add(o);
        }
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.remove(0);
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.get(0).name="WYM";
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
    }
    /**
     * 浅层copy方法3
     */
    public void copy3(ArrayList<Person> oldList){
        ArrayList<Person> newList = new ArrayList<Person>();
        newList.addAll(oldList);
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.remove(0);
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.get(0).name="WYM";
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
    }
    /**
     * 浅层copy4
     */
    public void copy4(ArrayList<Person> oldList){        
        ArrayList<Person> newList = new ArrayList<Person>(Arrays.asList(new Person[oldList.size()]));
        Collections.copy(newList, oldList);
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.remove(0);
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.get(0).name="WYM";
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
    } 
    /*
     * 深层拷贝
     */
    public void deepCopy(ArrayList<Person> oldList)throws Exception{
    	ByteArrayOutputStream baos=new ByteArrayOutputStream();
    	ObjectOutputStream oos=new ObjectOutputStream(baos);
    	oos.writeObject(oldList);
    	ByteArrayInputStream bais=new ByteArrayInputStream(baos.toByteArray());
    	ObjectInputStream ois=new ObjectInputStream(bais);
    	ArrayList<Person> newList=(ArrayList<Person>)ois.readObject();
    	System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.remove(0);
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
        oldList.get(0).name="WYM";
        System.out.println("oldList:"+oldList);
        System.out.println("newList:"+newList);
    	
    }
    public static void main(String[] args)throws Exception{
        Test1 test = new Test1();
        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person("A",1));
        list.add(new Person("B",2));
        list.add(new Person("C",3));
        //test.copy1(list);
        //test.copy2(list);
        //test.copy3(list);
        //test.copy4(list);
        test.deepCopy(list);
    }
}
class Person implements Serializable{
	String name;
	int age;
	public Person(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
	@Override
	public String toString() {
		return "姓名:"+this.name+"-年龄:"+this.age;
	}
}


### Java深拷贝浅拷贝的区别 在Java编程语言中,当涉及到对象的复制时,区分浅拷贝深拷贝至关重要。这两种方式处理对象及其内部成员变量的方式不同。 #### 浅拷贝 (Shallow Copy) 浅拷贝是指创建一个新的对象,该对象会拥有原有对象属性值的一份精确拷贝。对于基本数据类型的字段,其值会被直接复制到新对象中;而对于引用类型的数据,则只是简单地复制了引用地址而非实际的对象实例[^1]。这意味着两个对象将共享同一个被引用的对象,在这种情况下改变任何一个对象所持有的引用可能会间接影响另一个对象的状态。 ```java public class ShallowCopyExample implements Cloneable { private int value; private Object reference; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } ``` #### 深拷贝 (Deep Copy) 相比之下,深拷贝不仅复制了源对象本身还对其所有的子对象进行了独立副本的创建[^4]。因此通过这种方式得到的新对象其原型之间没有任何关联——即它们各自拥有一套完全独立的数据结构。这使得我们可以安全地操作这些副本而不用担心会影响到原来的对象。 要实现深拷贝通常有两种常见途径: - **序列化机制**:利用`Serializable`接口配合输入输出流来完成整个对象图谱的持久化过程后再反序列化回内存形成全新的实体。 ```java import java.io.*; class DeepCopyBySerialization implements Serializable { public static <T extends Serializable> T deepCopy(T object) { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bos); out.writeObject(object); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream in = new ObjectInputStream(bis); return (T)in.readObject(); } catch (IOException | ClassNotFoundException e) { throw new RuntimeException(e); } } } ``` - **手动克隆**:针对复杂嵌套结构的手工编写构造函数或工厂方法逐层递归构建每一个组成部分直至最底层节点为止。 ```java public final class DeepCloneableObject { private String name; private transient List<String> items; // 使用transient修饰符忽略某些不需要参深拷贝的过程 public DeepCloneableObject(String name, List<String> items){ this.name=name; if(items!=null)this.items=new ArrayList<>(items);else this.items=null; } public DeepCloneableObject deepClone(){ return new DeepCloneableObject(this.name,this.items==null? null :new ArrayList<>(this.items)); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值