ArrayList<GuideGroup> questionGuideGroupList = new ArrayList<GuideGroup>(Arrays.asList(new GuideGroup[guideGroupList.size()])); Collections.copy(questionGuideGroupList, guideGroupList); questionAnswerManInfo.setGuideGroupList(questionGuideGroupList);
經過Collections.copy方式進行拷貝必須先肯定list的長度。
java
ArrayList<GuideGroup>questionGuideGroupList = (ArrayList<GuideGroup>) guideGroupList.clone()
ArrayList<GuideGroup> questionGuideGroupList = new ArrayList<GuideGroup>(); <pre name="code" class="java"> questionGuideGroupList.addAll(questionGuideGroupList);
一、序列化javabeanide
a)、javabean 繼承Serializable 接口,容許javabean序列化。ui
b)、javabean 繼承Cloneable接口,同時必須實現clone()方法,clone()方法能夠直接飲用父類的clone()方法this
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(); } }
二、實現深度拷貝方法code
* 深度拷貝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; }
三、深度拷貝調用對象
/*對對象進行深度拷貝*/ 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); } } }