By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,793 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,793 IT Pros & Developers. It's quick & easy.

array verses ArrayList

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Tom Jones" <to********@hotmail.com> wrote in message
news:uf***************@TK2MSFTNGP10.phx.gbl...
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
Nov 16 '05 #2

P: n/a

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:uI*************@TK2MSFTNGP12.phx.gbl...
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
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.