[Newbies] Array vs OrderedCollection

David T. Lewis lewis at mail.msen.com
Mon Apr 27 10:31:10 UTC 2009


On Sun, Apr 26, 2009 at 10:08:54PM -0500, Miguel Enrique Cob? Mart?nez wrote:
> Jerome Peace wrote:
> >
> >Hi Miguel,
> >
> >The answer depends on what you are doing.
> >
> >Arrays are compact fixed size entities. OrderedCollections are less 
> >compact and less fixed size.
> >
> >If you are building a list by adding objects to it. Don't care to know the 
> >maximum size in advance. Or need a stack like object, OrderedCollections 
> >are your class of object.
> >
> >Once you are done building. Know the exact size and know that few changes 
> >to the list will be needed. Array is good.
> >
> >Many things should not care whether the collection is one or the other as 
> >far as messages go. Performance will usually favor Arrays when the size is 
> >not changing.
> >
> >So do you want random access to your collection?
> >Or do you need to easily add or remove things from the ends?
> >
> >It is easy to have it both ways. When the processing favors 
> >OrderedCollections use asOrderedCollection to insure the class.
> >When random access rather than growth is needed use asArray.
> >
> >Don't care? Leave it be. Most language will work with either.
> >
> >Also some objects already have preferences. Streams currently expect their 
> >contents to be Arrays.
> >Strings expect to be arrays of Characters.
> >
> >I have found no I-can-plan-in-advance type rules for this. I find myself 
> >annoyed when the code requires I actually choose.
> >
> >I usually aim for using Arrays. And if the code fights me on this I use 
> >OrderedCollections.
> >
> >If I want a stack then I need ordered collection using addLast: item and 
> >removeLast for push and pop. I found this out after reading Kent Beck's 
> >book on Smalltalk best practice pattens.
> >
> >Yours in curiosity and service, --Jerome Peace
> 
> Thanks for your advise. I think that as the array is a VM-aware type, it 
> is implemented as native code. Maybe that is not the case with the 
> ordered colection, that is byte code compiled.

Array is an ordinary class, just like OrderedCollection. The VM is aware
of class Array only in the sense that it has hooks to create instances of
Array from within the VM, but the actual instances are objects just like
other object.

> But besides that, a OrderedCollection is more versatile than an Array.
> Anyway, I think that for my code, as is more focused to adding and 
> removing objects and for this, an OrderedCollection is the best choice.

Yes, good choice.

Dave
 


More information about the Beginners mailing list