By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,894 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,894 IT Pros & Developers. It's quick & easy.

storing items in list/collection by an ID key

P: n/a
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
Oct 2 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
*/
-------------------------------------------------------------------

Oct 2 '06 #2

P: n/a

Jeroen wrote:
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.

Oct 2 '06 #3

P: n/a

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?

Oct 2 '06 #4

P: n/a
There's no obvious reason I see not to use foreach. Where did you get
that example code?

Jeroen wrote:
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?
Oct 2 '06 #5

P: n/a
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

Macca wrote:
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
Oct 2 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.