File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes ArrayList behaviour Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "ArrayList behaviour" Watch "ArrayList behaviour" New topic

ArrayList behaviour

Thomas Goorden
Ranch Hand

Joined: Aug 15, 2001
Posts: 39
I'm working with an method that should return an Arraylist. This ArrayList should contain a bunch of ArrayLists itself. To create those inner objects, I use a "template" (for lack of the correct word) object, of which the values get filled in after creating an instance.
However, I seems to have problems getting a "clone" of my template object, to work with. For some reason, the "clones" all get the same content. I've noticed that the "clone" method should not transfer any content at all (or am I mistaken?), instead just create an ArrayList of the same size. Instead, I get the exact opposite: not only do they all get content, it's the same as the last one that gets filled in. (All objects have the same content as the last one that gets added to the list)
The code in "condensed" form:

As stated, the House class itself is an extension of an ArrayList.
This application (well this part anyway) is really starting to bug me, please help!
[ April 15, 2002: Message edited by: Thomas Goorden ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
According to the API regarding the clone method of ArrayList:

Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)

Perhaps you misinterpreted this. When it says that the elements themselves are not copied, that doesn't mean that the new ArrayList won't be filled, it simply means that the new ArrayList will contain exactly the same references as the original ArrayList. Therefore, the contents of the original ArrayList aren't copied, they're just referenced from another place. That's the notion of a shallow copy. A deep copy would involve the duplication of all of the elements of the original ArrayList.
I hope this helps. If you have any more questions, please let me know.

SCJP Tipline, etc.
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
The clone method in ArrayList makes a shallow copy of the ArrayList, meaning you get a new ArrayList with all of its elements the same as the orginal. Where the API says that 'The elements themselves are not copied.' This means that there are no copies made of the original objects, the elements in the returned ArrayList are the some ones as in the orginal. You're not getting a new empty ArrayList you're getting a copy of the original one. To make it a 'deep' copy you would have to override the clone method to make new objects of each of the elements in the List so that each cloned List has its own unique elements that have the same values as the orginal List.
If all you are looking for is an empty ArrayList of the same size as the orginal then just create a new ArrayList.
Hope that helps

I agree. Here's the link:
subject: ArrayList behaviour
It's not a secret anymore!