[Newbies] Array vs OrderedCollection

Jerome Peace peace_the_dreamer at yahoo.com
Sun Apr 26 21:44:44 UTC 2009

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

--- On Sun, 4/26/09, Miguel Enrique Cobá Martínez <miguel.coba at gmail.com> wrote:

> From: Miguel Enrique Cobá Martínez <miguel.coba at gmail.com>
> Subject: [Newbies] Array vs OrderedCollection
> To: "A friendly place to get answers to even the most basic questions about Squeak." <beginners at lists.squeakfoundation.org>
> Date: Sunday, April 26, 2009, 5:00 PM
> Hi all,
> are there any reason to prefer:
> "adding 2 objects"
> array := Array with: anObject.
> array := array copyWith: aSecondObject
> "removing object"
> array := array copyWithout: aSecondObject.
> instead of:
> "adding 2 objects"
> oc := OrderedCollection new.
> oc add: anObject.
> oc add: aSecondObject.
> "removing object"
> oc remove: aSecondObject ifAbsent: []
> This in reference to
> Class >> addSubclass: and Class >>
> removeSubclass: methods
> Thanks for any answers
> Miguel Cobá
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners


More information about the Beginners mailing list