gpt4 book ai didi

java - Collections.copy(dest, src) 仍然引用源集合

转载 作者:太空宇宙 更新时间:2023-11-04 08:43:34 24 4
gpt4 key购买 nike

我在论坛中搜索了处理集合深度复制的方法,但在我手中,Collections.copy(dest, src) 无法按预期工作。我错过了什么吗?

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.getCellValues().toArray(new CellValue[studyValCol.getCellValues().size()]);
studyRecCol.getCellValues().addAll(new ArrayList<CellValue>(Arrays.asList(cellValArray)));
break;
}
}
}

谢谢

克里斯

最佳答案

Collections.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.

它只是进行浅复制(导致每个列表中复制引用到同一组对象)。

关于java - Collections.copy(dest, src) 仍然引用源集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4799377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com