ArrayList的深拷贝与浅拷贝

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

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


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

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;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值