ArrayList vs. List

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

    ArrayList vs. List

    I noticed in my code, which is essentially an api for the client, that
    I return ArrayList object to the client. In my case, list consist of
    some objects that user only need to iterate over, hence no
    modification to list is done after it filled by me.

    What do you think is the best data structure for this requirement?

    Thanks
  • Ignacio Machin ( .NET/ C# MVP )

    #2
    Re: ArrayList vs. List

    On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gm ail.comwrote:
    I noticed in my code, which is essentially an api for the client, that
    I return ArrayList object to the client. In my case, list consist of
    some objects that user only need to iterate over, hence no
    modification to list is done after it filled by me.
    >
    What do you think is the best data structure for this requirement?
    >
    Thanks
    You should return a readonly collection. Take a look at
    ReadOnlyCollect ion , it's in System.Collecti ons.ObjectModel

    Comment

    • puzzlecracker

      #3
      Re: ArrayList vs. List

      On Oct 16, 11:27 am, "Ignacio Machin ( .NET/ C# MVP )"
      <ignacio.mac... @gmail.comwrote :
      On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gm ail.comwrote:
      >
      I noticed in my code, which is essentially an api for the client, that
      I return ArrayList object to the client. In my case, list consist of
      some objects that user only need to iterate over, hence no
      modification to list is done after it filled by me.
      >
      What do you think is the best data structure for this requirement?
      >
      Thanks
      >
      You should return a readonly collection. Take a look at
      ReadOnlyCollect ion , it's in System.Collecti ons.ObjectModel
      I think it is part of 3.0+, I need to support .NET 2.0... suggestions?

      Comment

      • Mythran

        #4
        Re: ArrayList vs. List



        "puzzlecrac ker" <ironsel2000@gm ail.comwrote in message
        news:3815ca86-54a8-4039-b6b1-cb5ee2cd7ebd@k1 6g2000hsf.googl egroups.com...
        On Oct 16, 11:27 am, "Ignacio Machin ( .NET/ C# MVP )"
        <ignacio.mac... @gmail.comwrote :
        >On Oct 16, 11:05 am, puzzlecracker <ironsel2...@gm ail.comwrote:
        >>
        I noticed in my code, which is essentially an api for the client, that
        I return ArrayList object to the client. In my case, list consist of
        some objects that user only need to iterate over, hence no
        modification to list is done after it filled by me.
        >>
        What do you think is the best data structure for this requirement?
        >>
        Thanks
        >>
        >You should return a readonly collection. Take a look at
        >ReadOnlyCollec tion , it's in System.Collecti ons.ObjectModel
        >
        I think it is part of 3.0+, I need to support .NET 2.0... suggestions?
        Why not use an ArrayList internally and then convert the ArrayList to an
        array and return that?

        Kip

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: ArrayList vs. List

          On Oct 16, 5:04 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
          You should return a readonly collection. Take a look at
          ReadOnlyCollect ion , it's in System.Collecti ons.ObjectModel
          >
          I think it is part of 3.0+, I need to support .NET 2.0... suggestions?
          What makes you think that? From MSDN:


          <quote>
          Version Information
          ..NET Framework
          Supported in: 3.5, 3.0, 2.0
          </quote>

          Jon

          Comment

          • puzzlecracker

            #6
            Re: ArrayList vs. List

            On Oct 16, 12:41 pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
            On Oct 16, 5:04 pm, puzzlecracker <ironsel2...@gm ail.comwrote:
            >
            You should return a readonly collection. Take a look at
            ReadOnlyCollect ion , it's in System.Collecti ons.ObjectModel
            >
            I think it is part of 3.0+, I need to support .NET 2.0... suggestions?
            >
            What makes you think that? From MSDN:http://msdn.microsoft.com/en-us/library/ms132474.aspx
            >
            <quote>
            Version Information
            .NET Framework
            Supported in: 3.5, 3.0, 2.0
            </quote>
            >
            Jon
            I was wrong, it's supported... I first encountered it in the book
            on .Net 3.0, hence the erroneous assumption.

            Comment

            • Rudy Velthuis

              #7
              Re: ArrayList vs. List

              Mythran wrote:
              Why not use an ArrayList internally and then convert the ArrayList to
              an array and return that?
              Perhaps because "converting the ArrayList to an array" would mean
              copying data from the ArrayList to the array, perhaps even multiple
              times, and that is relatively slow, if compared to giving people direct
              (but readonly) access.

              --
              Rudy Velthuis http://rvelthuis.de

              "We totally deny the allegations, and we are trying to identify
              the allegators."

              Comment

              • puzzlecracker

                #8
                Re: ArrayList vs. List

                On Oct 16, 2:22 pm, "Rudy Velthuis" <newsgro...@rve lthuis.dewrote:
                Mythran wrote:
                Why not use an ArrayList internally and then convert the ArrayList to
                an array and return that?
                >
                Perhaps because "converting the ArrayList to an array" would mean
                copying data from the ArrayList to the array, perhaps even multiple
                times, and that is relatively slow, if compared to giving people direct
                (but readonly) access.
                >
                --
                Rudy Velthuis        http://rvelthuis.de
                >
                "We totally deny the allegations, and we are trying to identify
                 the allegators."
                Does arraylist uses a traditional array and List uses nodes to store
                elements, much like in Java?

                Comment

                • Peter Duniho

                  #9
                  Re: ArrayList vs. List

                  On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker <ironsel2000@gm ail.com>
                  wrote:
                  Does arraylist uses a traditional array and List uses nodes to store
                  elements, much like in Java?
                  ArrayList, List<T>, and Java's ArrayList all use arrays to store elements.

                  Comment

                  • Rudy Velthuis

                    #10
                    Re: ArrayList vs. List

                    puzzlecracker wrote:
                    Does arraylist uses a traditional array and List uses nodes to store
                    elements, much like in Java?
                    No, List<Tis simply the generic equivalent of ArrayList. Both
                    internally use an array to hold the list elements. But ArrayList uses
                    an array of objects, while List<Tuses an array of instances of type T.

                    --
                    Rudy Velthuis http://rvelthuis.de

                    "Barabási's Law of Programming: Program development ends when the
                    program does what you expect it to do — whether it is correct or
                    not." -- Albert-László Barabási

                    Comment

                    • Rudy Velthuis

                      #11
                      Re: ArrayList vs. List

                      Peter Duniho wrote:
                      On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker
                      <ironsel2000@gm ail.com wrote:
                      >
                      Does arraylist uses a traditional array and List uses nodes to
                      store elements, much like in Java?
                      >
                      ArrayList, List<T>, and Java's ArrayList all use arrays to store
                      elements.
                      Quite right, but I guess he meant that Java's List class is in fact
                      implemented as a doubly linked list.

                      --
                      Rudy Velthuis http://rvelthuis.de

                      "No Sane man will dance." -- Cicero (106-43 B.C.)

                      Comment

                      • Peter Duniho

                        #12
                        Re: ArrayList vs. List

                        On Thu, 16 Oct 2008 12:01:30 -0700, Rudy Velthuis
                        <newsgroups@rve lthuis.dewrote:
                        Peter Duniho wrote:
                        >
                        >On Thu, 16 Oct 2008 11:41:42 -0700, puzzlecracker
                        ><ironsel2000@g mail.com wrote:
                        >>
                        Does arraylist uses a traditional array and List uses nodes to
                        store elements, much like in Java?
                        >>
                        >ArrayList, List<T>, and Java's ArrayList all use arrays to store
                        >elements.
                        >
                        Quite right, but I guess he meant that Java's List class is in fact
                        implemented as a doubly linked list.
                        Java doesn't have a List class. It does have a doubly-linked list class
                        called LinkedList, and like the ArrayList class it does implement the List
                        interface. But "puzzlecrac ker" didn't mention any LinkedList class at
                        all, in Java or C#.

                        In any case, it's my hope that my reply answered his question, in spite of
                        the inaccuracies in that question.

                        Pete

                        Comment

                        • Rudy Velthuis

                          #13
                          Re: ArrayList vs. List

                          Peter Duniho wrote:
                          Quite right, but I guess he meant that Java's List class is in fact
                          implemented as a doubly linked list.
                          >
                          Java doesn't have a List class. It does have a doubly-linked list
                          class called LinkedList,
                          Hmmm... then I must be confusing it with one of the many other
                          frameworks I have seen lately. <g>

                          Sorry.

                          --
                          Rudy Velthuis http://rvelthuis.de

                          "A low voter turnout is an indication of fewer people going to
                          the polls." -- George W. Bush

                          Comment

                          • Ben Voigt [C++ MVP]

                            #14
                            Re: ArrayList vs. List

                            Rudy Velthuis wrote:
                            Peter Duniho wrote:
                            >
                            >>Quite right, but I guess he meant that Java's List class is in fact
                            >>implemented as a doubly linked list.
                            >>
                            >Java doesn't have a List class. It does have a doubly-linked list
                            >class called LinkedList,
                            >
                            Hmmm... then I must be confusing it with one of the many other
                            frameworks I have seen lately. <g>
                            C++ std::list<typen ame Tis a linked list like C# LinkedList<T>.
                            C++ std::vector<typ ename Tis a wrapped array like ArrayList or List<T>.
                            >
                            Sorry.

                            Comment

                            • Rudy Velthuis

                              #15
                              Re: ArrayList vs. List

                              Ben Voigt [C++ MVP] wrote:
                              Rudy Velthuis wrote:
                              Peter Duniho wrote:
                              Quite right, but I guess he meant that Java's List class is in
                              fact implemented as a doubly linked list.
                              >
                              Java doesn't have a List class. It does have a doubly-linked list
                              class called LinkedList,
                              Hmmm... then I must be confusing it with one of the many other
                              frameworks I have seen lately. <g>
                              >
                              C++ std::list<typen ame Tis a linked list like C# LinkedList<T>.
                              Ah, indeed. I guess I was indeed thinking of std::list<T>.
                              --
                              Rudy Velthuis http://rvelthuis.de

                              "Imagine if every Thursday your shoes exploded if you tied them
                              the usual way. This happens to us all the time with computers,
                              and nobody thinks of complaining." -- Jeff Raskin

                              Comment

                              Working...