storing items in list/collection by an ID key

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a number of objects i would like to store in a list like struture.

They already have ID's associated with them so i would like to utilise these
ids as accessors the list..

At the moment I have approx 10 objects with ids that are spread out,

e.g 1, 12, 24, 56, 200,320....

now i dont want to create a list with space for 320 items when there are
only 10 actual objects which is what I would have to do if i used the above
IDs as the key to store/access the objects.

I'm sure this is a common problem and that there are solutions to avoid this
waste of space.

I'd appreciate if anyone can give me advice on how to strore these items
efficently and how to access them from a list like structure,

Thanks
Macca
 
Have a look at the 'System.Collections' items. I guess in your example
especially the Hashtable would be interesting. Here's the example for
the .NET help of hashtables:

-------------------------------------------------------------------
using System;
using System.Collections;
public class SamplesHashtable {

public static void Main() {

// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("First", "Hello");
myHT.Add("Second", "World");
myHT.Add("Third", "!");

// Displays the properties and values of the Hashtable.
Console.WriteLine( "myHT" );
Console.WriteLine( " Count: {0}", myHT.Count );
Console.WriteLine( " Keys and Values:" );
PrintKeysAndValues( myHT );
}


public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key,
myEnumerator.Value);
Console.WriteLine();
}
}
/*
This code produces the following output.

myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
-------------------------------------------------------------------
 
Jeroen said:
Have a look at the 'System.Collections' items. I guess in your example
especially the Hashtable would be interesting. Here's the example for
the .NET help of hashtables:

-------------------------------------------------------------------
using System;
using System.Collections;
public class SamplesHashtable {

public static void Main() {

// Creates and initializes a new Hashtable.
Hashtable myHT = new Hashtable();
myHT.Add("First", "Hello");
myHT.Add("Second", "World");
myHT.Add("Third", "!");

// Displays the properties and values of the Hashtable.
Console.WriteLine( "myHT" );
Console.WriteLine( " Count: {0}", myHT.Count );
Console.WriteLine( " Keys and Values:" );
PrintKeysAndValues( myHT );
}


public static void PrintKeysAndValues( Hashtable myList ) {
IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
Console.WriteLine( "\t-KEY-\t-VALUE-" );
while ( myEnumerator.MoveNext() )
Console.WriteLine("\t{0}:\t{1}", myEnumerator.Key,
myEnumerator.Value);
Console.WriteLine();
}
}
/*
This code produces the following output.

myHT
Count: 3
Keys and Values:
-KEY- -VALUE-
Third: !
Second: World
First: Hello
*/
-------------------------------------------------------------------

Out of curiosity, why do you use an IDictionaryEnumerator?
foreach(DictionaryEntry d in myList) also works. The enumerator method
works fine, it just wouldn't be my first choice, so I'm wondering if
I'm missing out on something :)

To the OP, if you know the ID, the following also works.
Console.WriteLine( myList["First"].ToString());
Where "First" is they key of one of the items from the example posted
above.
 
DeveloperX schreef:
Out of curiosity, why do you use an IDictionaryEnumerator?
foreach(DictionaryEntry d in myList) also works. The enumerator method
works fine, it just wouldn't be my first choice, so I'm wondering if
I'm missing out on something :)

I'm afraid that I can't answer that. The problem posted initially
seemed to ask for hashtables. I just posted an example from the .NET
help, so it's actually not my code, I wouldn't know the answer to your
question.

Maybe someone else does?
 
There's no obvious reason I see not to use foreach. Where did you get
that example code?
 
Macca,
From context clues I'm assuming you'd like to have the objects ordered
by ID so that when you enumerate them the IDs are monotonically
increasing. Is that correct?

If that's the case then you'll probably want to use
System.Collections.SortedList or if you're 2.0 then use either
System.Collections.Generics.SortedList or
System.Collections.Generics.SortedDictionary. The difference between
the later two is that SortedList is implemented as an array so it use
less memory at the expense of slower insertions and deletions while
SortedDictionary is implemented as a red black tree so it has faster
insertions and deletions at the expensive of more memory.

If that's not the case and you really don't care about the order the
objects then use System.Collections.Hashtable or
System.Collections.Generics.Dictionary.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top