array verses ArrayList

  • Thread starter Thread starter Tom Jones
  • Start date Start date
T

Tom Jones

I have a class that contains a collection of reference types. This class
needs to have a method that returns the collection to the caller.

The method's signature could be either (it could also be implemented as a
property):

MyObject[] GetObjects();

or

ArrayList GetObjects();

The caller can modify the objects that are returned (meaning the caller
should not get a copy of the objects).

The size of the collection is never going to change.

Is there any reason why I would want to return the objects in an ArrayList
verses a plain array?

Thanks,
TJ
 
Tom Jones said:
I have a class that contains a collection of reference types. This class
needs to have a method that returns the collection to the caller.

The method's signature could be either (it could also be implemented as a
property):

MyObject[] GetObjects();

or

ArrayList GetObjects();

The caller can modify the objects that are returned (meaning the caller
should not get a copy of the objects).

The size of the collection is never going to change.

Is there any reason why I would want to return the objects in an ArrayList
verses a plain array?

If the size cannot change, then no, an Array of MyObject is preferable.
With an ArrayList the user will have to down-cast the objects to use them,
which is a small ugly hastle. You could implements a non-resizable
MyObjectCollection, but that seems a little silly. Even if you have to do
some resizing or array copying, remember that smallish arrays of reference
types hava a small memory footprint and are cheap to manipulate since they
are really just arrays of pointers.

David
 
David Browne said:
If the size cannot change, then no, an Array of MyObject is preferable.
With an ArrayList the user will have to down-cast the objects to use them,
which is a small ugly hastle. You could implements a non-resizable
MyObjectCollection, but that seems a little silly. Even if you have to do
some resizing or array copying, remember that smallish arrays of reference
types hava a small memory footprint and are cheap to manipulate since they
are really just arrays of pointers.

David


Thank you for your response David - I will go with the Array.

-TJ
 
Back
Top