This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Cloning Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Cloning Doubt" Watch "Cloning Doubt" New topic
Author

Cloning Doubt

Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
As per the API for the ArrayList's clone method, it says that:


According to what i have understood from the statement is that:
The ArrayList's elements inside the List are not copied, they point to the same reference. That is, if i change an element in the original arrayList, this change can as well be viewed in the Cloned List.
Am i right?

This program explains what Cloning in ArrayList does:

The output shows:


I have gone through the ArrayList code and they have used System.arrayCopy() method to copy the elements of the original ArrayList to the Cloned ArrayList. So the behavior, but why such behavior?
Doesnt this falsify the meaning of shallow cloning?
Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258
System.arrayCopy does a shallow copy also, so no it doesn't falsify the meaning of shallow cloning. For one thing, collections can't really make any assumptions about the types of objects that they hold. They may or may not be able to be clonable, so promising that an ArrayList will return a deep-copy would be stupid, since it may be impossible!


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cloning Doubt
 
Similar Threads
Cloning of Multidimensional Array
How do you guard your arrays?
clone of ArrayList
ArrayList and shallow cloning
Cloning 1D and 2D array