Home All Groups Group Topic Archive Search About

list of objects vs Array of objects

Author
3 Sep 2006 9:40 PM
John Devlon
Hi

Can anyony please tell me why I should use a List of objects instead of an
array of objects ?

Thanx

John

Author
3 Sep 2006 10:40 PM
Tom Shelton
John Devlon wrote:
> Hi
>
> Can anyony please tell me why I should use a List of objects instead of an
> array of objects ?
>
> Thanx
>
> John

Well, John when it comes right down to it - an array is a list.  I
suspect you mean an array with it's fixed size vs a more dynamic list
such as System.Collections.ArrayList.  Well, the only reason to use one
over the other has to do with the fixed size part :)

If you know exactly how many objects you want to store in you list, and
you know that is all your ever going to store - then by all means use
an array.  If you don't know how many you will have, then an arraylist
is probably a better choice.  That's not to say that you can't support
dynamic array implementations with an array.  You most certainly can -
you just have to use an efficient resize & copy algorithm (well you
don't have to, but your users will hate you if you don't :) .  Or you
can just use an ArrayList, which already does this for you :)

Anyway, HTH.

--
Tom Shelton
Author
4 Sep 2006 4:43 AM
Cor Ligthert [MVP]
Tom,

I would not have written this in your message.

>That's not to say that you can't support
> dynamic array implementations with an array.  You most certainly can -
> you just have to use an efficient resize & copy algorithm (well you
> don't have to, but your users will hate you if you don't :) .  Or you
> can just use an ArrayList, which already does this for you :)

The question was not "what is better" but "why"

For the rest I would have written than of course in my words the same as you
did.

Cor



Show quoteHide quote
"Tom Shelton" <t**@mtogden.com> schreef in bericht
news:1157323209.481618.45600@m79g2000cwm.googlegroups.com...
>
> John Devlon wrote:
>> Hi
>>
>> Can anyony please tell me why I should use a List of objects instead of
>> an
>> array of objects ?
>>
>> Thanx
>>
>> John
>
> Well, John when it comes right down to it - an array is a list.  I
> suspect you mean an array with it's fixed size vs a more dynamic list
> such as System.Collections.ArrayList.  Well, the only reason to use one
> over the other has to do with the fixed size part :)
>
> If you know exactly how many objects you want to store in you list, and
> you know that is all your ever going to store - then by all means use
> an array.  If you don't know how many you will have, then an arraylist
> is probably a better choice.  That's not to say that you can't support
> dynamic array implementations with an array.  You most certainly can -
> you just have to use an efficient resize & copy algorithm (well you
> don't have to, but your users will hate you if you don't :) .  Or you
> can just use an ArrayList, which already does this for you :)
>
> Anyway, HTH.
>
> --
> Tom Shelton
>
Author
4 Sep 2006 2:12 PM
Tom Shelton
Tom Shelton wrote:
Show quoteHide quote
> John Devlon wrote:
> > Hi
> >
> > Can anyony please tell me why I should use a List of objects instead of an
> > array of objects ?
> >
> > Thanx
> >
> > John
>
> Well, John when it comes right down to it - an array is a list.  I
> suspect you mean an array with it's fixed size vs a more dynamic list
> such as System.Collections.ArrayList.  Well, the only reason to use one
> over the other has to do with the fixed size part :)

Ok, thinking more about this there is one more consideration if you are
using VB.NET 2002 OR 2003...  The type of object that you are storing
in the list can be a factor as well.  If the list is potentially large,
and you are storing value types, then you would probably be better off
with implementing a custom collection wrapped around a dynamic array
implementation.  The reason is that when you use the standard ArrayList
your dealing with values of type object, and so incure boxing penalties
when working with the values stored in the list.

Of course, this isn't an issue with VB.NET 2005 if you use the generic
list implementation (System.Collections.Generic.List).

--
Tom Shelton