Collections.copy(dest, src) still Referencing Source Collection
I have searched the forum for handling deep-copying of collections but in my hands, Collections.copy(dest, src) isn't working as expected. Did I miss something?
List<Column> mergedStudies = new ArrayList<Column>(Arrays.asList(new Column[studyColumns.size()]));
Collections.copy(mergedStudies, studyColumns);
for (Iterator itrStudyRecColumns = mergedStudies.iterator(); itrStudyRecColumns.hasNext();) {
Column studyRecCol = (Column) itrStudyRecColumns.next();
for (Iterator itrStudyValColumns = studyValueColumns.iterator(); itrStudyValColumns.hasNext();) {
Column studyValCol = (Column) itrStudyValColumns.next();
if (studyRecCol.getColumnName().equals(studyValCol.getColumnName())) {
// Note: this method dereferences copies to an existing destination collection WITH items appended to end of collection.
CellValue[] cellValArray = studyValCol.getCellVa开发者_JAVA百科lues().toArray(new CellValue[studyValCol.getCellValues().size()]);
studyRecCol.getCellValues().addAll(new ArrayList<CellValue>(Arrays.asList(cellValArray)));
break;
}
}
}
Thanks,
Chris
Collections.copy()
doesn't (nor does it claim to) perform a deep copy:
Copies all of the elements from one list into another. After the operation, the index of each copied element in the destination list will be identical to its index in the source list. The destination list must be at least as long as the source list. If it is longer, the remaining elements in the destination list are unaffected.
It just does a shallow copy (resulting in copied references in each list to the same set of objects).
精彩评论