java复制arraylist_java - 如何将一个ArrayList的内容复制到另一个?

java – 如何将一个ArrayList的内容复制到另一个?

我有一些数据结构,我想使用一个作为临时,另一个不是临时的。

ArrayList myObject = new ArrayList();

ArrayList myTempObject = new ArrayList();

//fill myTempObject here

….

//make myObject contain the same values as myTempObject

myObject = myTempObject;

//free up memory by clearing myTempObject

myTempObject.clear();

现在这个问题当然是myTempObject真的只是指向myObject,所以一旦myTempObject被清除,那么是myObject。

如何使用java保留myTempObject中的值myObject?

10个解决方案

117 votes

你可以使用这样的技巧:

myObject = new ArrayList(myTempObject);

或使用

myObject = (ArrayList)myTempObject.clone();

您可以在此处获取有关clone()方法的一些信息

但你应该记住,所有这些方法都会给你一份你的List,而不是它的所有元素。 因此,如果您更改复制的列表中的某个元素,它也将在原始列表中更改。

Artem answered 2019-06-13T06:12:17Z

35 votes

originalArrayList.addAll(copyArrayList);

请注意:使用addAll()方法进行复制时,两个数组列表(originalArrayList和copyArrayList)的内容都引用相同的对象或内容。 因此,如果您修改其中任何一个,则另一个也将反映相同的更改。

如果你不想这样,那么你需要将每个元素从originalArrayList复制到copyArrayList,就像使用for或while循环一样。

Dhananjay M answered 2019-06-13T06:12:57Z

16 votes

通过赋值运算符在java中没有隐式副本。 变量包含一个引用值(指针),当你使用ArrayList时,你只需要处理该值。

为了保留ArrayList的内容,您需要复制它。

这可以通过使用另一个myTempObject的构造函数创建一个新的ArrayList来完成:

ArrayList myObject = new ArrayList(myTempObject);

编辑:波希米亚人在下面的评论中指出,这就是你要问的问题吗? 通过执行上述操作,ArrayList(myTempObject和myObject)都将包含对相同对象的引用。 如果您确实想要一个包含myTempObject中包含的对象的新副本的新列表,那么您需要复制原始文件中的每个单独对象ArrayList

Brian Roach answered 2019-06-13T06:13:49Z

7 votes

我自己也遇到了同样的问题。

说arraylist1 = arraylist2将它们设置为指向同一个地方,所以如果你改变了数据,那么两个列表总是保持不变。

要将值复制到独立列表中,我只使用foreach复制内容:

ArrayList list1 = new ArrayList();

ArrayList list2 = new ArrayList();

以你现在的任何方式填写list1。

foreach( obj in list1)

{

list2.Add(obj);

}

PaulC answered 2019-06-13T06:14:40Z

4 votes

您希望将oldList复制到名为newList的新ArrayList对象中

ArrayList newList = new ArrayList<>() ;

for (int i = 0 ; i

newList.add(oldList.get(i)) ;

}

这两个列表是独立的,对一个列表的更改不会反映到另一个列表中。

Java Main answered 2019-06-13T06:15:14Z

3 votes

你需要clone()个人对象。 Constructor和其他方法执行浅拷贝。 您可以尝试Collections.copy方法。

adatapost answered 2019-06-13T06:15:42Z

3 votes

让我们试试这个例子

ArrayList firstArrayList = new ArrayList<>();

firstArrayList.add(“One”);

firstArrayList.add(“Two”);

firstArrayList.add(“Three”);

firstArrayList.add(“Four”);

firstArrayList.add(“Five”);

firstArrayList.add(“Six”);

//copy array list content into another array list

ArrayList secondArrayList=new ArrayList<>();

secondArrayList.addAll(firstArrayList);

//print all the content of array list

Iterator itr = secondArrayList.iterator();

while (itr.hasNext()) {

System.out.println(itr.next());

}

在打印输出如下

我们也可以使用clone()方法来创建精确的副本

对于那个尝试你可以尝试像

**ArrayListsecondArrayList = (ArrayList) firstArrayList.clone();**

And then print by using iterator

**Iterator itr = secondArrayList.iterator();

while (itr.hasNext()) {

System.out.println(itr.next());

}**

Nilam Rajaput answered 2019-06-13T06:17:16Z

1 votes

直接制作原始列表的深层副本的方法是将所有元素从一个列表添加到另一个列表。

ArrayList originalList = new ArrayList();

ArrayList duplicateList = new ArrayList();

for(Object o : originalList) {

duplicateList.add(o);

}

现在如果对originalList进行任何更改,它将不会影响duplicateList。

Chirag Khimani answered 2019-06-13T06:17:52Z

0 votes

要将一个列表复制到另一个列表中,您可以使用调用的方法Collection.copy(myObject myTempObject)。现在执行这些代码后,你可以看到myObject中的所有列表值。

Rakhi Jaligama answered 2019-06-13T06:18:31Z

0 votes

将一个列表复制到第二个很简单,你可以这样做: –

ArrayList list1= new ArrayList<>();

ArrayList list2= new ArrayList<>();

//this will your copy your list1 into list2

list2.addAll(list1);

Imran samed answered 2019-06-13T06:19:01Z

    原文作者:weixin_39969568
    原文地址: https://blog.csdn.net/weixin_39969568/article/details/114463764
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞