• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList behaviour

 
Thomas Goorden
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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!
Thanks...
[ April 15, 2002: Message edited by: Thomas Goorden ]
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Corey
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic