|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
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 Why not use an ArrayList in the collections namespace, this already has the
member functions to do this. 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 > "Adam Honek" <AdamHo***@Webmaster2001.freeserve.co.uk> schrieb: Arrays are somewhat static data structures. If you have to add, insert, and > 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? 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/> 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? -- Show quoteHide quoteDennis in Houston "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/> > > Dennis wrote:
> How is an arraylist organized in memory. I know an array is simply like Internally an ArrayList holds its members in an array. The point is> building blocks stacked one after the other. Does an arraylist use pointers > to various memory locations where the blocks are scattered around throughout > memory? that it manages addition and removal for you. -- Larry Lard Replies to group please 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? -- Show quoteHide quoteDennis in Houston "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 > > 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 The ArrayList works very fast for adding elements as it doesn't need to>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? 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 wrote:
> If that's the case, then there is really no "speed advantage" except for any The point of an ArrayList isn't a speed advantage - the point is that> optimizatin that M'soft does in the ArrayList Class. 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 You can use TrimToSize to make an ArrayList trim its array down to the> 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? size of the list. -- Larry Lard Replies to group please |
|||||||||||||||||||||||