我有两个具有相同类型的arraylist对象。想要通过进行必要的更改来将一个数据复制到另一个。但这会改变我的两个对象。以下是代码。
private ArrayList<Class_Summery_Enrollment> arrangeRecords(ArrayList<Class_Summery_Enrollment> source){
ArrayList<Class_Summery_Enrollment> records = new ArrayList<Class_Summery_Enrollment>();
ArrayList<Class_Meeting_Pattern_Enrollment> pattersList = new ArrayList<Class_Meeting_Pattern_Enrollment>();
Class_Meeting_Pattern_Enrollment meetingPatternRec;
Class_Summery_Enrollment rec = new Class_Summery_Enrollment();
for(int i = 0 ;i<source.size();i++){
if(source.get(i).getClass_MEETING_PATTERNS().size()>1){
Log.v(Constants.TAG, "Total Patterns = "+source.get(i).getClass_MEETING_PATTERNS().size());
int size_ = source.get(i).getClass_MEETING_PATTERNS().size();
for(int c = 0;c<size_;c++){
rec = new Class_Summery_Enrollment();
rec = source.get(i);
if(c==0){
for (int k =0;k<source.get(i).getClass_MEETING_PATTERNS().size();k++){
pattersList.add(source.get(i).getClass_MEETING_PATTERNS().get(k));
}
}
rec.getClass_MEETING_PATTERNS().get(0).setSsr_INSTR_LONG(pattersList.get(c).getSsr_INSTR_LONG());
rec.getClass_MEETING_PATTERNS().get(0).setSsr_MTG_LOC_LONG(pattersList.get(c).getSsr_MTG_LOC_LONG());
rec.getClass_MEETING_PATTERNS().get(0).setSsr_MTG_DT_LONG(pattersList.get(c).getSsr_MTG_DT_LONG());
rec.getClass_MEETING_PATTERNS().get(0).setSsr_MTG_SCHED_LONG(pattersList.get(c).getSsr_MTG_SCHED_LONG());
records.add(rec);
}
}else{
records.add(source.get(i));
}
}
return records;
}
答案 0 :(得分:1)
在Java中,当您说Object o1= o2;
时,这意味着您正在尝试创建一个名为o1
的新对象,其地址与o2
相同。 SO ,您实际上只创建了一个对象,并在其上引用了两个对象。修改此对象将反映对象本身,并从任何对象地址访问它将产生相同的结果。
您需要使用Deep Copy
的概念,手动复制真实值,而不是参考,Article来理解它
答案 1 :(得分:0)
我通过在构造函数中传递自定义对象来解决它。在上面的代码中我分配了对其他对象的引用,因为它们都指向同一位置。以下代码将帮助您。
enter code here
private ArrayList<NormalizedClassRecords> reArrangeRecs( ArrayList<Class_Enrollment> source){
ArrayList<NormalizedClassRecords> records = new ArrayList<NormalizedClassRecords>();
NormalizedClassRecords normalizedRec = new NormalizedClassRecords();
for(int i=0;i<source.size();i++){
NormalizedClassRecords rec = new NormalizedClassRecords(normalizedRec);
rec.setCourseId(source.get(i).getCourseId());
rec.setDescription(source.get(i).getDescription());
for(int j=0;j<source.get(i).getAdviseclasses().size();j++){
NormalizedClassRecords rec2 = new NormalizedClassRecords(rec);
rec2.setClassnbr(source.get(i).getAdviseclasses().get(j).getClassnbr());
rec2.setRoomLocation(source.get(i).getAdviseclasses().get(j).getRoomLocation());
rec2.setSubjectArea(source.get(i).getAdviseclasses().get(j).getSubjectArea());
rec2.setSection(source.get(i).getAdviseclasses().get(j).getSection());
rec2.setCatalogNbr(source.get(i).getAdviseclasses().get(j).getCatalogNbr());
for(int k=0;k<source.get(i).getAdviseclasses().get(j).getMeetingPat().size();k++){
NormalizedClassRecords rec3 = new NormalizedClassRecords(rec2);
rec3.setInstructorName(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getInstructorName());
rec3.setMeetingStartTime(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getMeetingStartTime());
rec3.setMeetingEndTime(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getMeetingEndTime());
rec3.setStartDate(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getStartDate());
rec3.setEndDate(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getEndDate());
rec3.setMonday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getMonday());
rec3.setTuesday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getTuesday());
rec3.setWednesday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getWednesday());
rec3.setThursday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getThursday());
rec3.setFriday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getFriday());
rec3.setSaturday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getSaturday());
rec3.setSunday(source.get(i).getAdviseclasses().get(j).getMeetingPat().get(k).getSunday());
records.add(rec3);
}
}
}
return records;
}