• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help cloning ArrayList Objects.

 
Ranch Hand
Posts: 37
Netbeans IDE Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Netbeans IDE Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I solved with brute force:
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic