How to copy the contents of one ArrayList to another?

I have some data structures, and I would like to use one of them as temporary, and the other as not temporary.

ArrayList<Object> myObject = new ArrayList<Object>(); ArrayList<Object> myTempObject = new ArrayList<Object>(); //fill myTempObject here .... //make myObject contain the same values as myTempObject myObject = myTempObject; //free up memory by clearing myTempObject myTempObject.clear(); 

now the problem with this, of course, is that myObject really just points to myTempObject , and so once myTempObject is cleared, so myObject .

How to save values ​​from myTempObject to myObject using java?

+56
java variable-assignment reference
Dec 09 '11 at 5:59
source share
10 answers

You can use this trick:

 myObject = new ArrayList<Object>(myTempObject); 

or use

 myObject = (ArrayList<Object>)myTempObject.clone(); 

You can get some information about the clone () method here

But you must remember that all these methods will give you a copy of your list , and not all of its elements. Therefore, if you change one of the elements in the copied list, it will also be changed in your original list.

+98
Dec 09 '11 at 6:01
source share

originalArrayList addAll (copyArrayList) ;.

Pay attention . When using the addAll () method to copy, the contents of both lists of arrays (originalArrayList and copyArrayList) refer to the same objects or contents. Therefore, if you change one of them, the other will also reflect the same changes.

If you do not, you need to copy each element from the originalArrayList to copyArrayList, for example, using a for or while loop.

+31
Mar 13 '15 at 13:42
source share

There are no statements in java using the assignment operator. Variables contain a reference value (pointer), and when you use = , you only deal with that value.

To save the contents of myTempObject , you will need to make a copy of it.

This can be done by creating a new ArrayList using a constructor that accepts another ArrayList :

 ArrayList<Object> myObject = new ArrayList<Object>(myTempObject); 

Edit: As the Bohemians noted in the comments below, is this what you are asking? By doing the above, both ArrayList ( myTempObject and myObject ) will contain references to the same objects. If you really need a new list containing new copies of the objects contained in myTempObject , then you will need to make a copy of each individual object in the original ArrayList

+14
Dec 09 '11 at 6:03
source share

I came to this while encountering the same problem.

Speaking arraylist1 = arraylist2, they both point to the same place, so if you change or the data changes, and therefore both lists always remain unchanged.

To copy the values ​​to an independent list, I simply used foreach to copy the contents:

 ArrayList list1 = new ArrayList(); ArrayList list2 = new ArrayList(); 

fill out list1 no matter how you are.

 foreach(<type> obj in list1) { list2.Add(obj); } 
+5
Apr 10 '15 at 17:10
source share

Suppose you want to copy oldList to a new ArrayList named newList

 ArrayList<Object> newList = new ArrayList<>() ; for (int i = 0 ; i<oldList.size();i++){ newList.add(oldList.get(i)) ; } 

These two lists are indepedant, changes to one do not affect the other.

+4
Jan 6 '16 at 20:27
source share

You need clone() separate object. Constructor and other methods perform a shallow copy. You can try Collections.copy .

+3
Dec 09 '11 at 6:05
source share
  Lets try the example ArrayList<String> 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<String> 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()); } In print **output** as below One Two Three Four Fivexach Six We can also do by using **clone()** method for which is used to create exact copy for that try you can try as like **ArrayList<String>secondArrayList = (ArrayList<String>) firstArrayList.clone();** And then print by using iterator **Iterator itr = secondArrayList.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); }** 
+2
Oct 27 '17 at 5:24 on
source share

to copy one list to another list, you can use a method called Collection.copy (myObject myTempObject). Now after executing this line of code u you can see all the list values ​​in myObject.

0
Feb 07 '14 at 7:11
source share

Copying one list to the second is quite simple, you can do it as shown below: -

 ArrayList<List1> list1= new ArrayList<>(); ArrayList<List1> list2= new ArrayList<>(); //this will your copy your list1 into list2 list2.addAll(list1); 
0
Nov 15 '17 at 11:53 on
source share

An easy way to make a deep copy of the original list is to add all the items from one list to another list.

 ArrayList<Object> originalList = new ArrayList<Object>(); ArrayList<Object> duplicateList = new ArrayList<Object>(); for(Object o : originalList) { duplicateList.add(o); } 

Now, if you make any changes to the originalList, this will not affect the duplicateList.

0
Nov 23 '17 at 5:32
source share



All Articles