Re: array verses ArrayList
From: David Browne (meat_at_hotmail.com)
Date: 08/21/04
- Next message: Philippe Bertrand: "Compiler resolves to wrong overloaded constructor or method"
- Previous message: Patrick Altman: "Re: HOWTO: Read and Write the Registry to persist State"
- In reply to: Tom Jones: "array verses ArrayList"
- Next in thread: Tom Jones: "Re: array verses ArrayList"
- Reply: Tom Jones: "Re: array verses ArrayList"
- Messages sorted by: [ date ] [ thread ]
Date: Fri, 20 Aug 2004 20:05:57 -0500
"Tom Jones" <tomjones16@hotmail.com> wrote in message
news:uf%23YJBxhEHA.964@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
- Next message: Philippe Bertrand: "Compiler resolves to wrong overloaded constructor or method"
- Previous message: Patrick Altman: "Re: HOWTO: Read and Write the Registry to persist State"
- In reply to: Tom Jones: "array verses ArrayList"
- Next in thread: Tom Jones: "Re: array verses ArrayList"
- Reply: Tom Jones: "Re: array verses ArrayList"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|