8

With 2 ArrayList, I was wondering if the best way from transforming the 1st one into a "copy" of the second one is to go like

myFirstArray.clear();
myFirstArray.addAll(mySecondArray);

or

myFirstArray = mySecondArray.clone();

What are the main differences between those two method, which on is preferrable and is there another "easier" or "cleaner" solution. Thanks for any tips

EDIT : I use this copy for replacing an Array of item im currently working with the one where I store the item I'll work with in the next loop. At the end of the loop I replace my currentArrayList with my futurArrayList and I clear my futurArraylist in order to add new item in it (i hope its clear enough)

WizLiz
  • 2,068
  • 5
  • 25
  • 39
  • Be careful if your list is a bunch of reference types you may end up with lists pointing to the same object. – Kevin DiTraglia Jul 23 '13 at 12:17
  • 2
    Just a style comment: generally your variable names should start with a lower-case letter. Not only for people reading the code, but also the stackoverflow syntax highlighting gets confused! – Alex MDC Jul 23 '13 at 12:18
  • What is the goal of your copy? Knowing what you want to do will help us give you the best answer. – Jonathan Drapeau Jul 23 '13 at 12:18

4 Answers4

13

The first one replaces the content of the list by another content. The second one creates another ArrayList instance, leaving the previous one untouched.

If the list is referenced by some other object, and you want this other object to be untouched, use the second one. If you want the other object to also have the new content, use the first one.

If nothing else referenced the list, it doesn't matter much. The second one will reduce the memory used in case you replace the content of a huge list by a few elements.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Thanks for the precision, my Arraylist is indeed referenced by another object so addAll is preferrable. – WizLiz Jul 23 '13 at 12:28
1

Use that:

List<Object> first = ...
ArrayList<Object> second = new ArrayList<>(first);

I also suggest that you do not use clone() at all. It's better to use a copy constructor or some factory method. Take a look at here.

Of course in your case, with the ArrayList, it will work as expected, you will end up with a copy of the references.

Community
  • 1
  • 1
Ortwin Angermeier
  • 5,957
  • 2
  • 34
  • 34
  • i dont want the reference copy. i want to create a new list and edit it. how can iu do it? – Sagar Nayak Mar 05 '18 at 10:57
  • If you want a deep copy of the list, you have to create a copy for each object in the list. I suggest you create a copy constructor for your objects and then just iterate the old one and populate the new one. – Ortwin Angermeier Mar 05 '18 at 17:12
1

In java, though clone is ‘intended’ to produce a copy of the same object it is not guaranteed. Clone comes with lots of its and buts. So my first advice is to not depend on clones.

By default, java cloning is ‘field by field copy’ i.e. as the Object class does not have idea about the structure of class on which clone() method will be invoked. So, JVM when called for cloning, do following things:

  1. If the class has only primitive data type members then a completely new copy of the object will be created and the reference to the new object copy will be returned.

  2. If the class contains members of any class type then only the object references to those members are copied and hence the member references in both the original object as well as the cloned object refer to the same object because of that cloned object changes are visible in original also.

Ankur Lathi
  • 7,636
  • 5
  • 37
  • 49
  • In my case, copying just the referrence isn't a real issue because I intend to modify those object so having the same reference is beneficial. But thanks for the precision – WizLiz Jul 23 '13 at 12:34
0

Guava, guava, guava!

final List copied = ImmutableList.copyOf(originalList);
jfly
  • 7,715
  • 3
  • 35
  • 65
gyorgyabraham
  • 2,550
  • 1
  • 28
  • 46