ArrayList的深度copy和浅度拷贝

来源:互联网 发布:abcmouse软件下载 编辑:程序博客网 时间:2024/06/06 02:46

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




0 0
原创粉丝点击