• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help cloning ArrayList Objects.  RSS feed

 
Josh Borg
Ranch Hand
Posts: 37
Chrome Eclipse IDE Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I want to clone some Arraylist, but the compiler apparently are just referencing the values to it's original ArrayList. I don't know what should it be:


Notice that ArrayList dailyMeal should be untouchable, but it return the changes that I made in local for-loop iteration although I didn't added nothing to it, just to it's clone.

Output:

Thanks for your helping.
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Take a look at what the ArrayList.clone() method does. You can do this by looking it up in the javadoc API, or if you are using Eclipse, hover over the code ".clone()".

What you need is a deep copy. Google "java deep copy" to see what this is, or search StackOverflow.com.

When an element is added to an ArrayList, a reference to an object is added, not the object itself. This is confusing because people often speak in shorthand: "add an object." That really means, "Add a reference to an object." To see what I mean, consider this code:



What will be output? Why?
 
Josh Borg
Ranch Hand
Posts: 37
Chrome Eclipse IDE Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:Take a look at what the ArrayList.clone() method does. You can do this by looking it up in the javadoc API, or if you are using Eclipse, hover over the code ".clone()".

What you need is a deep copy. Google "java deep copy" to see what this is, or search StackOverflow.com.

When an element is added to an ArrayList, a reference to an object is added, not the object itself. This is confusing because people often speak in shorthand: "add an object." That really means, "Add a reference to an object." To see what I mean, consider this code:



What will be output? Why?


This is different thing, you didn't cloned it.
 
Josh Borg
Ranch Hand
Posts: 37
Chrome Eclipse IDE Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved with brute force:
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why take a clone? There is a better way to copy Lists: this. You get a read‑only view of the List, but it reflects an changes made to the original List.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Josh Borg wrote:I solved with brute force:...

Which is almost always the wrong way to go.

You actually have more than one problem at play here:
1. Your ArrayList is not typed.
2. You are adding the List to your clone instead of its elements.

And the two are linked. If your List had been properly typed, you would never have been able to make the second mistake. As for point 2, I suggest you look at the addAll() method of java.util.List.

And just FYI, there is a third problem: You are using Strings to define food items, which is not generally a good idea. It may be OK for a simple scenario, but it's likely to run you into trouble if you ever decide to do anything more sophisticated. For more info you can read the StringsAreBad page.

HIH

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!