我有一个嵌套的ArrayList
,形式如下:
ArrayList<ArrayList<PointF>> nestedArraylist
我想创建一个“副本”nestedArraylistCopy
,其意义如下:
nestedArraylistCopy
的元素应该是nestedArraylist
元素的独立副本,即应该是ArrayList,其中包含对原始nestedArraylist
中相同PointF
对象的引用。
Can I somehow use
Collections.copy(dest, src)
to do what I want? The documentation is not exactly detailed unfortunately...Does the following code do what I want?
for(int i = 0; i < nestedArraylist.size(); i++) nestedArraylistCopy.add(new ArrayList<PointF>(nestedArraylist.get(i)));
Is there a more efficient and or elegant solution?
Collections.copy()
和更加优雅的解决方案,所以我认为这会使得这个问题更加完整和有用。 - cgogolinList<PointF>
不同的对象,但与源列表共享相同的PointF
引用。那么你在Q2中的方法应该是可以的。但请记住,如果你在复制的列表中“更改”了PointF
,元素源列表也会被更改。顺便说一句,这很容易测试。 - Kent