Removing array entry so cumbersome??

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

    #1

    Removing array entry so cumbersome??

    Is there a direct way to remove one entry from a one dimensional array?

    I keep looking but either my eyes are funny or it isn't there.

    Must we really create a temp array and copy all but 1 of the entries over to
    delete one entry?

    All I want to do is remove an entry at a specified index and then have the
    ubound(array) go down -1.

    Thanks in advance,
    Adam


  • OHM \( One Handed Man \)

    #2
    Re: Removing array entry so cumbersome??

    Why not use an ArrayList in the collections namespace, this already has the
    member functions to do this.


    --
    ( OHM ) - One Handed Man
    AKA Terry Burns - http://TrainingOn.net
    "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> wrote in message
    news:%23Buup3ac GHA.3856@TK2MSF TNGP03.phx.gbl. ..[color=blue]
    > Is there a direct way to remove one entry from a one dimensional array?
    >
    > I keep looking but either my eyes are funny or it isn't there.
    >
    > Must we really create a temp array and copy all but 1 of the entries over
    > to delete one entry?
    >
    > All I want to do is remove an entry at a specified index and then have the
    > ubound(array) go down -1.
    >
    > Thanks in advance,
    > Adam
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Removing array entry so cumbersome??

      "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:[color=blue]
      > Is there a direct way to remove one entry from a one dimensional array?
      >
      > I keep looking but either my eyes are funny or it isn't there.
      >
      > Must we really create a temp array and copy all but 1 of the entries over
      > to delete one entry?[/color]

      Arrays are somewhat static data structures. If you have to add, insert, and
      remove items often, you'd better use a dynamic data structure like
      'ArrayList' or 'List(Of T)' in VB 2005.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Dennis

        #4
        Re: Removing array entry so cumbersome??

        How is an arraylist organized in memory. I know an array is simply like
        building blocks stacked one after the other. Does an arraylist use pointers
        to various memory locations where the blocks are scattered around throughout
        memory?
        --
        Dennis in Houston


        "Herfried K. Wagner [MVP]" wrote:
        [color=blue]
        > "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:[color=green]
        > > Is there a direct way to remove one entry from a one dimensional array?
        > >
        > > I keep looking but either my eyes are funny or it isn't there.
        > >
        > > Must we really create a temp array and copy all but 1 of the entries over
        > > to delete one entry?[/color]
        >
        > Arrays are somewhat static data structures. If you have to add, insert, and
        > remove items often, you'd better use a dynamic data structure like
        > 'ArrayList' or 'List(Of T)' in VB 2005.
        >
        > --
        > M S Herfried K. Wagner
        > M V P <URL:http://dotnet.mvps.org/>
        > V B <URL:http://classicvb.org/petition/>
        >
        >[/color]

        Comment

        • Larry Lard

          #5
          Re: Removing array entry so cumbersome??


          Dennis wrote:[color=blue]
          > How is an arraylist organized in memory. I know an array is simply like
          > building blocks stacked one after the other. Does an arraylist use pointers
          > to various memory locations where the blocks are scattered around throughout
          > memory?[/color]

          Internally an ArrayList holds its members in an array. The point is
          that it manages addition and removal for you.

          --
          Larry Lard
          Replies to group please

          Comment

          • Dennis

            #6
            Re: Removing array entry so cumbersome??

            If that's the case, then there is really no "speed advantage" except for any
            optimizatin that M'soft does in the ArrayList Class. Then there is one point
            that bothers me and that's the way I understand the arraylist works. For
            example, it starts out with 16 elements when a New Arraylist is declared.
            When you try to set the 17th element, the arraylist doubles in size to 32 and
            so on. So, for example, if you have set the 40,000th element and that
            happens to trigger an expansion of the array pointed to by the arraylist,
            then you will end up with 80,000 elements. This would end up with an 80,000
            element array storage, abiet only with pointers to a nothing object. If I
            use the redim on an array when it gets full, I probably would end up with a
            lot less array elements in the end. Is this correct?
            --
            Dennis in Houston


            "Larry Lard" wrote:
            [color=blue]
            >
            > Dennis wrote:[color=green]
            > > How is an arraylist organized in memory. I know an array is simply like
            > > building blocks stacked one after the other. Does an arraylist use pointers
            > > to various memory locations where the blocks are scattered around throughout
            > > memory?[/color]
            >
            > Internally an ArrayList holds its members in an array. The point is
            > that it manages addition and removal for you.
            >
            > --
            > Larry Lard
            > Replies to group please
            >
            >[/color]

            Comment

            • Chris Chilvers

              #7
              Re: Removing array entry so cumbersome??

              On Tue, 9 May 2006 16:21:01 -0700, Dennis
              <Dennis@discuss ions.microsoft. com> wrote:
              [color=blue]
              >If that's the case, then there is really no "speed advantage" except for any
              >optimizatin that M'soft does in the ArrayList Class. Then there is one point
              >that bothers me and that's the way I understand the arraylist works. For
              >example, it starts out with 16 elements when a New Arraylist is declared.
              >When you try to set the 17th element, the arraylist doubles in size to 32 and
              >so on. So, for example, if you have set the 40,000th element and that
              >happens to trigger an expansion of the array pointed to by the arraylist,
              >then you will end up with 80,000 elements. This would end up with an 80,000
              >element array storage, abiet only with pointers to a nothing object. If I
              >use the redim on an array when it gets full, I probably would end up with a
              >lot less array elements in the end. Is this correct?[/color]

              The ArrayList works very fast for adding elements as it doesn't need to
              resize the array ever time it adds a new element. If you have an array
              with 40,000 elements in it and it runs out of space, in all likeliness
              you are going to want a large array as you are storing a lot.

              If you have just finished some sort of bulk insert and want to compact
              the array you could use ArrayList.TrimT oSize() which reduces the size of
              the array to the size of the data it stores.

              As for speed improvments, and the copying issue on removing an item (or
              inserting in the middle of an array). There is no way round that, this
              is a limitation of an array.

              Comment

              • Larry Lard

                #8
                Re: Removing array entry so cumbersome??


                Dennis wrote:[color=blue]
                > If that's the case, then there is really no "speed advantage" except for any
                > optimizatin that M'soft does in the ArrayList Class.[/color]

                The point of an ArrayList isn't a speed advantage - the point is that
                it encapsulates the details for you, so you can use it as a list.

                There's nothing in the collection classes that does any magic
                optimization. The speed advantage comes in not having to write the code
                yourself.
                [color=blue]
                > Then there is one point
                > that bothers me and that's the way I understand the arraylist works. For
                > example, it starts out with 16 elements when a New Arraylist is declared.
                > When you try to set the 17th element, the arraylist doubles in size to 32 and
                > so on. So, for example, if you have set the 40,000th element and that
                > happens to trigger an expansion of the array pointed to by the arraylist,
                > then you will end up with 80,000 elements. This would end up with an 80,000
                > element array storage, abiet only with pointers to a nothing object. If I
                > use the redim on an array when it gets full, I probably would end up with a
                > lot less array elements in the end. Is this correct?[/color]

                You can use TrimToSize to make an ArrayList trim its array down to the
                size of the list.

                --
                Larry Lard
                Replies to group please

                Comment

                Working...