Removing array entry so cumbersome??

  • Thread starter Thread starter Adam Honek
  • Start date Start date
A

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
 
Why not use an ArrayList in the collections namespace, this already has the
member functions to do this.
 
Adam Honek said:
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.
 
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 said:
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.
 
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?
 
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.
 
Dennis said:
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.
 
Back
Top