Re: Snapshot of List [Was: Re: sync on local variable]



Daniel Pitts wrote:
On 3/26/2010 7:22 AM, Eric Sosman wrote:
On 3/25/2010 3:28 PM, Daniel Pitts wrote:
On 3/25/2010 11:06 AM, Eric Sosman wrote:
>> [... about iterating over a changing List ...]
to keep the
iteration self-consistent you might want to do something like
lock ALL_ROWS, grab a snapshot with toArray(), unlock, and run
the iteration on the (private, stable) snapshot.
>
I agree except, don't use toArray, use new ArrayList<Row>();

Not confrontational, just curious: Why prefer a new ArrayList
to an array? To me, it appears that an ArrayList is just an array
wrapped up in extra machinery, and I can't see that the machinery
adds any value for this usage. So, why pay the extra freight?
What am I missing?
Array is a relatively primitive concept. You're question is akin to asking why use a "Date object" instead of an "int representing the milliseconds since Jan 1, 1970".

Either one "works". Yes, the non-primitive has more "machinery", but that isn't automatically a bad thing. The primitive looses semantic meaning outside of its context, where a properly designed abstraction maintains its semantics regardless of context.

Lists are also easier to work with, and work in more places.

I tend to use an array where the underlying structure I am representing really is an array, and a collection where the underlying structure really is a list or set or whatever.

Specifically I am not going to use an ArrayList, for example, if the thing being modelled is not resizable. Why would I want to write code to ensure that the ArrayList will stay at a fixed size when an aray will already take care of that for me?

AHS
.