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 Java in General and the fly likes new ArrayList or list.clone()? 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 » Java » Java in General
Bookmark "new ArrayList or list.clone()?" Watch "new ArrayList or list.clone()?" New topic

new ArrayList or list.clone()?

Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

I create new lists all over the place. Typically due to firing events and trying not to do it from inside a synchronized block, and not wanting to iterate across an accessible list while i am firing an event.

Typically I see that Sun uses clone. And I suppose that makes sense in a way. You know what your implementation is so you can use clone(). Is clone faster or cheaper than new ArrayList(oldlist)??? i use new ArrayList now.
Chris Montgomery
Ranch Hand

Joined: Jan 14, 2004
Posts: 141
would it make sense to use "javap" to see what the bytecode looks like?
Chris Montgomery
Ranch Hand

Joined: Jan 14, 2004
Posts: 141
don't know if this is helpful, but came up with this when I ran javap -c Test... looks like clone requries one less step.


Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Mmmm, I don't think javap tells us much useful here. The number of steps isn't really relevant unless we know what the steps are. Is it quicker to do A and B, or just C? Who knows; we'd need to analyze what A, B, and C really involve. If we really care I think it would be far more productive to run some timed tests. However I will go out on a limb here and guess that it's unlikely there will be a significant difference.

I prefer to avoid clone() here because I try to use the most general type I can to refer to classes I'm working with. If I've got an ArrayList instance, I prefer to declare it as a List, Collection, or Iterable (my first choice in many cases nowadays) unless I really need to declare a more specific type. And the thing is, none of those types declare a public clone() method. (Or copy() or whatever you prefer.) If I need to make a copy of a list, I can always use an ArrayList() constructor for that, since it's defined to work on any Collection. I don't have to require the pre-existing list to be an overly specific type. So that's the main reason I would avoid clone() here.
[ November 03, 2005: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Thanks for the tips.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Back to your original problem... If you are using the current version of Java (1.5),
you can use CopyOnWriteArrayList -- with it, you can safely iterator on the list while modifying it concurrently.
[ November 03, 2005: Message edited by: Jeff Albrechtsen ]

There is no emoticon for what I am feeling!
Don't get me started about those stupid light bulbs.
subject: new ArrayList or list.clone()?
Similar Threads
clone problems
Problems with addAll() from ArrayList - same reference...
Help with Arrays and ArrayLists
Cloning an ArrayList of ArrayStacks of Objects