"复制"嵌套的数组列表

4

我有一个嵌套的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?


3
关于你的第二个问题,你可以很容易地进行测试。 - Maroun
是的,我可以并且已经测试了第二个选项,并认为它有效。然而,我想问一下关于Collections.copy()和更加优雅的解决方案,所以我认为这会使得这个问题更加完整和有用。 - cgogolin
事实上,我不理解你问题中加粗的两个单词,“独立”和“相同”。 - Kent
独立 = 如果您修改了nestedArraylist的元素,则不应更改nestedArraylistCopy的元素。 相同 = 如果您修改了nestedArraylist中一个ArrayList中的PointF,则nestedArraylistCopy中相应的PointF应更改。 - cgogolin
@cgogolin,听起来你想要List<PointF>不同的对象,但与源列表共享相同的PointF引用。那么你在Q2中的方法应该是可以的。但请记住,如果你在复制的列表中“更改”了PointF,元素源列表也会被更改。顺便说一句,这很容易测试。 - Kent
3个回答

3
Q1: 当你使用Collections.copy后,你的新List对象将包含与src相同的元素(假设大小相同),这意味着它保存了相同的ArrayList<PointF>对象,因此PointF对象也是相同的。如果您无法从api java文档获得所需信息,请阅读源代码。
Q2: 你所做的不同于Collections.copy,因为你复制的arrayList具有新的Arraylist<PointF>作为元素,但它们包含与源列表相同的元素(PointF)。
Q3: 我不知道,因为我不知道你最终想要什么。所有新的对象?只有ArrayList应该是新的对象,还是所有引用都是新的?

非常感谢您解释了Collections.copy()。虽然我不是专家,但下次在询问之前我会仔细研究它的实现。在Q2中所描述的正是我想要达到的目标。这也是我在问题中试图用“独立”和“相同”这两个词来描述的。 - cgogolin

1
据我所知,您的解决方案仅更新引用,就像Collection.copy()一样。您可以使用下面的方法,这是我更喜欢的方法:
 List<ArrayList<PointF>> newList = new ArrayList<ArrayList<PointF>>(oldList);

更改旧列表不会影响新列表。

我测试了您的第二个选项,它也具有这样的属性:对旧列表的更改不会影响新列表。

注意 - 这些也只会更新引用。如果您更改新列表中的元素,它也会更新旧列表。我认为您的第二个数组将创建全新的对象,但我不能百分之百确定。我在下面添加了我的测试代码,供您参考。

package test;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Lasitha Benaragama on 4/28/14.
 */
public class StckTest {

public static void main(String[] args) {
    List<String> oldList = new ArrayList<String>();
    oldList.add("AAAAA");
    oldList.add("BBBBB");
    oldList.add("CCCCC");
    oldList.add("DDDDDD");
    oldList.add("EEEEEE");

    StckTest test = new StckTest();

    List<String> newListCopy = new ArrayList<String>(oldList);
    List<String> newListClone = new ArrayList<String>();

    for(int i = 0; i < oldList.size(); i++)
        newListClone.add(oldList.get(i));

    test.printArray(newListCopy);

    test.changeList(oldList);
    test.printArray(oldList);

    test.printArray(newListCopy);
    test.printArray(newListClone);
}

public void changeList(List<String> oldList) {
    oldList.remove(2);
    oldList.add("FFFFF");
}

public void printArray(List<String> oldList){
    for(int i = 0; i < oldList.size(); i++){
        System.out.print(oldList.get(i)+",");
    }
    System.out.println();
}

}

0
你可以使用 clone() 方法来创建对象的浅层副本。
        ArrayList<ArrayList<String>> nestedArraylist=new ArrayList<ArrayList<String>>();
        ArrayList<String> x=new ArrayList<String>();
        x.add("Deepak");
        nestedArraylist.add(x);
        System.out.println(nestedArraylist);
        ArrayList<ArrayList<String>> nestedArraylistcopy=(ArrayList<ArrayList<String>>)nestedArraylist.clone();
        System.out.println(nestedArraylistcopy);

谢谢,但那并没有真正回答我的问题。我想要nestedArraylistCopy的元素是nestedArraylist中元素的独立副本 - cgogolin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接