list of objects vs Array of objects

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Devlon

    list of objects vs Array of objects


    Hi

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

    Thanx

    John



  • Tom Shelton

    #2
    Re: list of objects vs Array of objects


    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.Collecti ons.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

    Comment

    • Cor Ligthert [MVP]

      #3
      Re: list of objects vs Array of objects

      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 Shelton" <tom@mtogden.co mschreef in bericht
      news:1157323209 .481618.45600@m 79g2000cwm.goog legroups.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.Collecti ons.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
      >

      Comment

      • Tom Shelton

        #4
        Re: list of objects vs Array of objects


        Tom Shelton wrote:
        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.Collecti ons.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.Collect ions.Generic.Li st).

        --
        Tom Shelton

        Comment

        Working...