ArrayList的浅度拷贝方式:

  • 通过Collections.copy方法实现浅度拷贝
       ArrayList<GuideGroup> questionGuideGroupList = new ArrayList<GuideGroup>(Arrays.asList(new GuideGroup[guideGroupList.size()]));
Collections.copy(questionGuideGroupList, guideGroupList);
questionAnswerManInfo.setGuideGroupList(questionGuideGroupList);

通过Collections.copy方式进行拷贝必须先确定list的长度。

  • 通过ArrayList.clone进行浅度拷贝
       ArrayList<GuideGroup>questionGuideGroupList = (ArrayList<GuideGroup>) guideGroupList.clone()
  • ArrayList.addAll实现浅度拷贝
       ArrayList<GuideGroup> questionGuideGroupList = new ArrayList<GuideGroup>();
<pre name="code" class="java"> questionGuideGroupList.addAll(questionGuideGroupList);

ArrayList深度拷贝方式

  • 通过序列化方式进行深度拷贝

1、序列化javabean

a)、javabean 继承Serializable 接口,允许javabean序列化。

b)、javabean 继承Cloneable接口,同时必须实现clone()方法,clone()方法可以直接饮用父类的clone()方法

public class GuideGroup implements Cloneable, Serializable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private String groupKey;
    private String groupName;
    private int groupUserCount;
    private int groupCorrectCount;
    private int groupWrongCount;
    private int groupUnTestedCount;
    private String groupCorrectRate;
    private String groupWrongRate;
    private String groupUnTestedRate;
    private List<GuideGroupUser> guideGroupUserList;
    
    /**
     * @return the groupKey
     */
    public String getGroupKey() {
        return groupKey;
    }
    /**
     * @param groupKey the groupKey to set
     */
    public void setGroupKey(String groupKey) {
        this.groupKey = groupKey;
    }
    /**
     * @return the groupName
     */
    public String getGroupName() {
        return groupName;
    }
    /**
     * @param groupName the groupName to set
     */
    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }
    /**
     * @return the groupCorrectCount
     */
    public int getGroupCorrectCount() {
        return groupCorrectCount;
    }
    /**
     * @param groupCorrectCount the groupCorrectCount to set
     */
    public void setGroupCorrectCount(int groupCorrectCount) {
        this.groupCorrectCount = groupCorrectCount;
    }
    /**
     * @return the groupWrongCount
     */
    public int getGroupWrongCount() {
        return groupWrongCount;
    }
    /**
     * @param groupWrongCount the groupWrongCount to set
     */
    public void setGroupWrongCount(int groupWrongCount) {
        this.groupWrongCount = groupWrongCount;
    }
    /**
     * @return the groupUnTestedCount
     */
    public int getGroupUnTestedCount() {
        return groupUnTestedCount;
    }
    /**
     * @param groupUnTestedCount the groupUnTestedCount to set
     */
    public void setGroupUnTestedCount(int groupUnTestedCount) {
        this.groupUnTestedCount = groupUnTestedCount;
    }
    /**
     * @return the groupCorrectRate
     */
    public String getGroupCorrectRate() {
        return groupCorrectRate;
    }
    /**
     * @param groupCorrectRate the groupCorrectRate to set
     */
    public void setGroupCorrectRate(String groupCorrectRate) {
        this.groupCorrectRate = groupCorrectRate;
    }
    /**
     * @return the groupWrongRate
     */
    public String getGroupWrongRate() {
        return groupWrongRate;
    }
    /**
     * @param groupWrongRate the groupWrongRate to set
     */
    public void setGroupWrongRate(String groupWrongRate) {
        this.groupWrongRate = groupWrongRate;
    }
    /**
     * @return the groupUnTestedRate
     */
    public String getGroupUnTestedRate() {
        return groupUnTestedRate;
    }
    /**
     * @param groupUnTestedRate the groupUnTestedRate to set
     */
    public void setGroupUnTestedRate(String groupUnTestedRate) {
        this.groupUnTestedRate = groupUnTestedRate;
    }
    public int getGroupUserCount() {
        return groupUserCount;
    }
    public void setGroupUserCount(int groupUserCount) {
        this.groupUserCount = groupUserCount;
    }
    public List<GuideGroupUser> getGuideGroupUserList() {
        return guideGroupUserList;
    }
    public void setGuideGroupUserList(List<GuideGroupUser> guideGroupUserList) {
        this.guideGroupUserList = guideGroupUserList;
    }    
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }        
}

2、实现深度拷贝方法

     * 深度拷贝list
     * 要求对对象进行序列化,并实现Cloneable接口
     * */
    public static List<?> deepCopy(List<?> src) {
        try{
            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;
        }catch(IOException e){
            e.printStackTrace();
        }catch(ClassNotFoundException e){
            e.printStackTrace();
        }         return null;
    }   

3、深度拷贝调用

     /*对对象进行深度拷贝*/
ArrayList<GuideGroup> questionGuideGroupList = (ArrayList<GuideGroup>) deepCopy(guideGroupList);
questionAnswerManInfo.setGuideGroupList(questionGuideGroupList);
  • 通过递归方式实现深度拷贝

通过递归方式,使用add方法实现深度拷贝

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

ArrayList的深度copy和浅度拷贝的更多相关文章

  1. List的深度copy和浅度拷贝

    List<Student> list= Arrays.asList( new Student("Fndroid", 22, Student.Sax.MALE, 180) ...

  2. C#深度拷贝和浅度拷贝方法

    C#浅度拷贝多用于值类型的复制,即 int a=1;int b=a; 设置b=2后不会影响a的值. 但如果对于引用类型class a=new class(); class b=a; 设置b.name= ...

  3. Java中深度克隆和浅度克隆

    一:使用目的: 就是为了快速构造一个和已有对象相同的副本.如果需要克隆对象,一般需要先创建一个对象,然后将原对象中的数据导入到新创建的对象中去,而不用根据已有对象进行手动赋值操作. 二:Object中 ...

  4. Java的深度克隆和浅度克隆

    说到克隆,其实是个比较简单的概念,跟现实生活正的克隆一样,复制一个一模一样的对象出来.clone()这个方法是从Object继承下来的,一个对象要实现克隆,需要实现一个叫做Cloneable的接口,这 ...

  5. .NET基础之深度复制和浅度复制

    之前一直没有搞清楚深度复制和浅度复制的区别到底在哪里,今天彻底把这个东西弄懂了,写出来与到家共勉. 如果大家不懂值类型和引用类型的区别,请先看http://www.cnblogs.com/Autumo ...

  6. scp(secure copy)安全拷贝

    scp(secure copy)安全拷贝 (1)scp定义: scp可以实现服务器与服务器之间的数据拷贝.(from server1 to server2) (2)基本语法 命令  递归  要拷贝的文 ...

  7. Cloneable接口的作用与深度克隆与浅度克隆

    cloneable接口的作用 cloneable其实就是一个标记接口,只有实现这个接口后,然后在类中重写Object中的clone方法,然后通过类调用clone方法才能克隆成功,如果不实现这个接口,则 ...

  8. C#深度复制和浅度复制

    C#深度复制和浅度复制 复制一个值变量很简单,新建一个变量然后将原来的变量赋值过去就行,但是复制一个引用变量这种方法是不行的,如果不明白为什么可以先看看这篇解释 引用类型变量和值类型变量在赋值时的不同 ...

  9. LeetCode----Copy List with Random Pointer 深度拷贝,浅度拷贝,Lazy拷贝解析

    题目:A linked list is given such that each node contains an additional random pointer which could poin ...

随机推荐

  1. 结构体 row_prebuilt_t

    typedef struct row_prebuilt_struct row_prebuilt_t; /** A struct for (sometimes lazily) prebuilt stru ...

  2. 【转】Java 内存模型及GC原理

    一个优秀Java程序员,必须了解Java内存模型.GC工作原理,以及如何优化GC的性能.与GC进行有限的交互,有一些应用程序对性能要求较高,例如嵌入式系统.实时系统等,只有全面提升内存的管理效率,才能 ...

  3. Art-Directing SVG图像viewBox属性

    Art-Directing SVG图像viewBox属性 作者:彦子 日期:2015-06-02 点击:992 svg 译者注:根据Google Dev文档的解释,Art Direction在这篇文章 ...

  4. (转载)file_get_contents("php://input")

    (转载)http://taoshi.blog.51cto.com/1724747/1165499 $data = file_get_contents("php://input"); ...

  5. Alexander Grothendieck去世了

    Alexander Grothendieck (German: [ˈɡroːtn̩diːk]; French: [ɡʁɔtɛndik]; 28 March 1928 – 13 November 201 ...

  6. [liu yanling]测试方法

    1.定义 是把所有可能的输入数据,即程序的输入域划分成若干部分(子集),然后从每一个子集中选取少数具有代表性的数据作为测试用例.该方法是一种重要的,常用的黑盒测试用例设计方法. 2.划分等价类 等价类 ...

  7. 华为2015 简单 字典输入法 java

    题目摘自http://blog.csdn.net/dongyi91/article/details/38639915 写了2个小时,水平太菜了 入法的编码原理为:根据已有编码表,当输入拼音和数字后输出 ...

  8. wuzhicms刷新按钮的功能开发

    这个刷新按钮可以刷新当前框架的页面. 但有的页面使用了弹窗打开后,再点击刷新就会打开之前的弹窗页面. 如: 再刷新的时候,这个框架内容就变了.而这里,我们实际需要刷新的是列表页面 打开这个程序的具体文 ...

  9. CF_402B 想法题

    题目链接:http://codeforces.com/problemset/problem/402/B /**算法分析: 题意太大意,positive没注意这个问题 考察等差数列,由An=A1+(n- ...

  10. [一]java环境变量的配置

    1.JAVA_HOME(新建):D:\jdk1.6 2.classpath(新建): .;%JAVA_HOME%\lib;%JAVA_HOME%\lib\tools.jar; 3.path(新增):% ...