Home All Groups Group Topic Archive Search About

Removing array entry so cumbersome??

Author
7 May 2006 8:11 AM
Adam Honek
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

Author
7 May 2006 9:56 AM
OHM ( One Handed Man )
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
Show quoteHide quote
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> wrote in message
news:%23Buup3acGHA.3856@TK2MSFTNGP03.phx.gbl...
> 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
>
Author
7 May 2006 10:35 AM
Herfried K. Wagner [MVP]
"Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
> 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?

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/>
Author
9 May 2006 12:37 AM
Dennis
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


Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb:
> > 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?
>
> 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/>
>
>
Author
9 May 2006 8:54 AM
Larry Lard
Dennis wrote:
> 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?

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
Author
9 May 2006 11:21 PM
Dennis
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


Show quoteHide quote
"Larry Lard" wrote:

>
> Dennis wrote:
> > 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?
>
> 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
>
>
Author
10 May 2006 1:10 AM
Chris Chilvers
On Tue, 9 May 2006 16:21:01 -0700, Dennis
<Den***@discussions.microsoft.com> wrote:

>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?

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.TrimToSize() 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.
Author
10 May 2006 9:48 AM
Larry Lard
Dennis wrote:
> If that's the case, then there is really no "speed advantage" except for any
> optimizatin that M'soft does in the ArrayList Class.

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.

> 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?

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

--
Larry Lard
Replies to group please