精简深拷贝ArrayList实例(包括递归和序列化方法)
2007-07-12 16:50
作者fbysss
msn:[email protected]  
blog:blog.csdn.net/fbysss
声明:本文由fbysss原创,转载请注明出处

关键字:深拷贝,序列化

前言:
        日前一哥们问我一个有关多层ArrayList拷贝的问题,我帮他写了一个例程,感觉以后用得着,便放上来了。如果要在自身类中加入Clone功能,需要implements ICloneable接口,然后用下面的相应代码重写clone方法即可。
源代码:
package com.sss.test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/** *//**
*

标题:


*

功能描述:

*
*


*

版权: Copyright (c) 2007


*

公司:


* @author fbysss
* @version 1.0
*
*


*

修改记录:


* 创建时间:2007-7-12 13:34:56
* 类说明
*/
public class DeepCopy ...{
     //深拷贝1:递归方法
     public void copy(List src,List dest)...{
         for (int i = 0 ;i < src.size() ; i++) ...{
             Object obj = src.get(i);            
             if (obj instanceof List)...{
                 dest.add(new ArrayList());
                     copy((List)obj,(List)((List)dest).get(i));
             }else...{
                 dest.add(obj);
             }
         }
        
     }
    
    
     //深拷贝2:序列化|反序列化方法
     public List copyBySerialize(List src) throws IOException, ClassNotFoundException...{
         ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
         ObjectOutputStream out = new ObjectOutputStream(byteOut);
         out.writeObject(src);
    
         ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
         ObjectInputStream in =new ObjectInputStream(byteIn);
         List dest = (List)in.readObject();
         return dest;
     }
    
     //浅拷贝
     public void copyByAdd(List src,List dest)...{
         //for (Object obj : src) {//jdk 1.5 以上版本        
         for (int i = 0 ; i< src.size() ;i++) ...{//jdk 1.4
             Object obj = src.get(i);
             dest.add(obj);
         }
        
     }    
     //添加引用
     public void evalByAdd(List src,List dest)...{
         dest.add(src);
     }
     //直接引用
     public List evalByRef(List src)...{
         return src;
     }    
     /** *//**
      * @param args
      */
     public static void main(String[] args) ...{
         List srcList = new ArrayList();
         List srcSubList1 = new ArrayList();
         srcSubList1.add("subItem1-1");
         srcSubList1.add("subItem1-2");
         srcSubList1.add("subItem1-3");
         List srcSubList2 = new ArrayList();
         srcSubList2.add("subItem2-1");
         srcSubList2.add("subItem2-2");
         srcSubList2.add("subItem2-3");        
         srcList.add(srcSubList1);
         srcList.add(srcSubList2);
        
         List destList = new ArrayList();
         DeepCopy dc = new DeepCopy();
         /** *//***********test#1*******************/
         //dc.copy(srcList,destList);
        
         /** *//***********test#2*******************/
         /**//*
         try {
             destList = dc.copyBySerialize(srcList);
         } catch (IOException e) {
             e.printStackTrace();
         } catch (ClassNotFoundException e) {            
             e.printStackTrace();
         }
         */
         /** *//***********test#3*******************/
         dc.copyByAdd(srcList,destList);
         ((List)srcList.get(0)).remove(0);
         /** *//***********test#4*******************/
         /**//*
         destList = dc.evalByRef(srcList);        
         */
         srcList.remove(1);
         dc.printList(destList);
        
        
     }
     private void printList(List destList) ...{
         //for (Object obj : destList) {//jdk 1.5 以上版本
         for (int i = 0 ; i< destList.size() ;i++) ...{//jdk 1.4
             Object obj = destList.get(i);
             if (obj instanceof List)...{
                 List listObj = (List)obj;
                     printList((List)listObj);
             }else...{
                 System.out.println(obj.toString());
             }
         }
        
     }
}

运行结果:
test#1和test#2由于是深拷贝,输出的都是完整的元素;
test#3为浅拷贝, 不受 srcList.remove( 1 );的影响,但是一旦加了((List)srcList.get( 0 )).remove( 0 );就会发现少了第一个元素;
test#4完全是引用src,src的任何改动都会影响dest的输出。