Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Dear All,

 

 

Just a quick question about collections and indexes...

 

Do collections retain their order permenantly via their index? For instance, if I have a collection with 10 most popular vegetables and the fourth is deleted, then do all of the vegetables from 4 to 9 get moved up by 1? If they do, then is there any chance that, excepting through some bad threading, the indexes would become shuffled in any way?

 

 

Thanks in advance,

Paul.

  • *Experts*
Posted

I'm not sure what Index you're talking about. There are many collection types, such as a Hashtable or ArrayList or SortedList. These usually have a key instead of an index.

 

If you're using ArrayList and have 3 items (0, 1, and 2) and you remove item 1, you will only have items 0 and 1 leftover. In other words, the items in the array shift down -- but there's no index about which to worry.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Hi Nerseus,

 

 

I'm actually referring to the Collection class itself. I can define an object as being of type Collection - the lowest of the low, I suppose, collection wise. I can then add objects to the collection and refer to an index to pick up the one that I need. I don't need the functionality of any of the ArrayList or HashTable classes - a simple Collection will suffice.

 

I suppose, the question I'm asking here is whether or not the order of the objects within the class become destabalised when you perform functions on them.

 

 

Thanks,

Paul.

  • Administrators
Posted

The Collection class is part of the VisualBasic namespace and IIRC it uses a HashTable internally anyway, you might as well go ahead and use on of the classes under System.Collections instead.

As far as I'm aware removing an item from an ArrayList simply causes the items to be shifted down - so the order should be consistent; however as tis isn't documented anywhere as being the 'correct' behaviour there is no guarentee this will not change in the future.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Experts*
Posted

I'm still not sure what you're trying to do with the collection where an Index would matter. To me, index only has meaning in an array where an item has a known index. The Collection class can reference the items by key or index but the index works just like an array. Meaning, if you have index 1 , 2 and 3 and remove index 1, the others will shift down.

 

Maybe you could show some code of what you want to accomplish and we could make a recommendation.

 

You have mentioned the "order" of items in the collection a few times. I wonder if you're concerned about the index-based order or a sorting order? If you want a sorted list, use the class called SortedList, part of the System.Collections namespace. If you want the indexes to not shift around, you're mostly out of luck unless you keep track of an index inside of your object, as a property of the object you put into the collection.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted (edited)

I'm building a stack onto which I can throw various objects and then pull them off using a pointer (hence the index). I don't want to have to mess around with the resizing of the stack if I can help it - the stack needs to be as small as possible to fit onto various types of PDA which may be slightly memory challenged. Using the Collection.Count - 1, I can easily aquire the stack top to pull the object that I'm after.

 

Do you think a HashTable would suit my needs more, or should I use an Array?

 

I will admit that on a PC version of one of the controls I've written for the system, I have utilised an array of objects and doubled the size of the array when it's discovered to be at its capacity:

'Just make sure we're not going to exceed the current array bounds...
If stackTop >= stackObject.GetUpperBound(0) Then
   ReDim Preserve stackPointer(stackObject.GetUpperBound(0) * 2)
   ReDim Preserve stackObject(stackObject.GetUpperBound(0) * 2)
End If

 

In this case, the array starts with a base of 16 elements, but I don't actually expect a whole lot more, here...

 

I know that popping a vlaue out of the middle of a stack does break the stack principal, but this is something that I may have to do depending on external interactions.

 

To be honest, I think I may simply refer to a Hash Table - I don't need any sorting except for the original LIFO stack. If this doesn't work, then I'll have to find a way to minimise the need to expand an array.

 

Don't worry about it guys I'll take a longer look in the help files. I think I spend my life in the, sometimes!

 

Thank-you to you both.

 

Paul.

Edited by mandelbrot
  • *Experts*
Posted

If you really need to remove something from the middle, maybe try an ArrayList. You can get to the last item easily and let's you remove from the middle.

 

Both Stack and ArrayList should be pretty lightweight.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Thanks to you both - you've both opened my eyes even wider (they should be popping out any day now!). As I said - there are so many classes and constructs.

 

I've reviewed the way that I'm writing this section of the project, and will incorporate some locking, so that the data series that I'm going to work with will be inaccessible to anyone else except the current user.

 

Thanks again, Nerseus & PD.

 

Paul.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...