1
如何複製包含ArrayList
成員的對象的ArrayList
。也有可能,列表中的對象的成員還包含ArrayList
成員,等等。那麼是否有一種通用的方法來複制對象列表,無論其成員是什麼?使用列表成員複製對象列表
對所有涉及的對象實施Clone Interface
似乎是一個壞主意,因爲I read that its broken。
我創建了一個小SSCCE來舉例說明我的問題:
class Foobar {
public List<String> listMember;
public String primitiveMember;
public Foobar() {
listMember = new ArrayList<String>(Arrays.asList("a", "b", "c"));
primitiveMember = "testABC";
}
}
// Create list of Foobar
List<Foobar> foobars = new ArrayList<Foobar>();
foobars.add(new Foobar());
// Copy list of Foobar
List<Foobar> foobarsCopy = new ArrayList<Foobar>();
foobarsCopy.add(null);
Collections.copy(foobarsCopy, foobars);
// Modify source list
foobars.get(0).listMember.add("-1-");
// Output of references
System.out.println("Sourcelist has size of: " + foobars.get(0).listMember.size());
System.out.println("Destinationlist has size of: " + foobarsCopy.get(0).listMember.size());
System.out.println("Function 'Collections.copy(...) does not copy list members of members?: " + (foobars.get(0).listMember.size() == foobarsCopy.get(0).listMember.size()) + " - References: " + foobars.get(0).listMember.hashCode() + " vs " + foobarsCopy.get(0).listMember.hashCode());
oK,您的解決方案看起來不錯,複製構造函數似乎是解決我的問題的好方法。但我認爲您的示例中的以下行不是必需的:foobarsCopy.add(new Foobar(new ArrayList(Arrays.asList(「d」,「e」,「f」)),「foo」)); –
alex
不,這不是必需的。我添加它來修改列表的副本,我認爲這樣會更清楚發生了什麼... ... –