list of objects vs Array of objects

  • Thread starter Thread starter John Devlon
  • Start date Start date
J

John Devlon

Hi

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

Thanx

John
 
John said:
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,

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
 
Tom said:
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).
 
Back
Top