【问题标题】:"Copy" nested array list“复制”嵌套数组列表
【发布时间】:2014-04-28 09:27:25
【问题描述】:

我有一个嵌套的ArrayList 形式

ArrayList<ArrayList<PointF>> nestedArraylist

我想在以下意义上创建nestedArraylist 的“副本”nestedArraylistCopy

nestedArraylistCopy 的元素应该是nestedArraylist 中元素的独立 副本,即应该是 ArrayLists,其中包含对 相同 PointF 对象的引用原nestedArraylist

  • 我能以某种方式使用Collections.copy(dest, src) 来做我想做的事吗?不幸的是,文档并不十分详细...

  • 下面的代码能满足我的要求吗?

    for(int i = 0; i < nestedArraylist.size(); i++) 
        nestedArraylistCopy.add(new ArrayList<PointF>(nestedArraylist.get(i)));
    
  • 有没有更高效、更优雅的解决方案?

【问题讨论】:

  • 关于你的第二个问题,你可以很容易地测试它..
  • 是的,我可以并且已经测试了第二个选项并认为它有效。但是,我想问一下Collections.copy() 和一个更优雅的解决方案,所以我认为它会使这个问题更完整和有用。
  • 其实我没看懂你问题里的两个粗体字independentsame....
  • independent = 如果你修改了nestedArraylist 的元素,nestedArraylistCopy 的元素不应该改变same = 如果你修改了其中一个ArrayLists 中的PointF nestedArraylist,那么nestedArraylistCopy中ArrayLists中对应的PointF应该会改变。
  • @cgogolin 听起来你想要List&lt;PointF&gt; 不同的对象,但与源列表共享相同的PointF ref。那么你在第二季度的方式应该没问题。但请记住,如果您“更改”了复制列表中的 PointF,则元素源列表也会更改。顺便说一句,这可以很容易地测试。

标签: java collections arraylist deep-copy


【解决方案1】:

Q1:在你 Collections.copy 之后,你的新 List 对象将包含与 src 相同的元素(假设大小相同),这意味着它拥有相同的 ArrayList&lt;PointF&gt; 对象,因此 PointF 对象是也一样。如果您无法从 api java doc 中获取您想要的信息,请阅读源代码。

Q2:您所做的与 Collections.copy 不同,因为您复制的 arrayList 具有新的 Arraylist&lt;PointF&gt; 作为元素,但它们包含与源列表相同的元素 (PointF)。

Q3:我不知道。因为我不知道你最终想要什么。所有新对象?只有 ArrayList 应该是新对象,还是所有引用?

【讨论】:

  • 感谢Collections.copy() 的解释。我不是专家,但下次询问之前会先研究一下实现。您在 Q2 下描述的正是我想要实现的。这就是我试图在我的问题中使用 independentsame 这两个词来描述的内容。
【解决方案2】:

据我所知,您的解决方案将只更新引用,Collection.copy() 也是如此。您可以使用以下我更喜欢的方法:

 List<ArrayList<PointF>> newList = new ArrayList<ArrayList<PointF>>(oldList);

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

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

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

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();
}

}

【讨论】:

    【解决方案3】:

    您可以使用 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 的元素的独立副本。
    猜你喜欢
    • 2011-02-02
    • 2019-05-23
    • 1970-01-01
    • 2018-06-10
    • 2012-01-09
    相关资源
    最近更新 更多