I have 100,000 strings each with a fixed ordered index value like this:
Index String Value
0 XXXXXXXXXXXXXXXXXXXXX
1 XXXXXXXXXX
2 (empty string)
3 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
4 XXXXX
5 XXXXXXXXX
6 XXXXXXXXXXXXXXX
7 (empty string)
8 XX
9 XXXXXXXXXX
10 XXXXXXXXXXXXXXXXXXXXXXXXXX
... ...
99999 XXXXXXXXXXXXXXXXXXX
My data structure must hold exactly 100,000 ordered entries and some (or many) of the string values may be empty, at least initially. Each index value will be unique (sequential integers) and, with the exception of empty strings, each string value will also be unique. For display purposes in my UI, I’d normally just populate my data structure, bind the listbox to it (with a DisplayMember and ValueMember specified). But in this case, I only want to display the strings that are not empty. So presumably, I’ll need to iterate through my data structure and add the applicable items to a listbox in a manner similar to this:
foreach (item in MyDataStructure)
{
if (item.StringValue != string.Empty)
{
listBox1.Items.Add(item);
}
}
It’s very important for me to be able to maintain the relationship between each string and its index value at all times. As you might expect, my users will need to add/edit/delete strings. In theory, all three operations boil down to the same thing: updating a string value at a particular index. To add a new string, I’ll need to first iterate through my data structure and make sure that there’s an empty string somewhere so I can replace it with the new string. If no empty strings exist, my user will need to “edit” an existing string or “delete” another string first because we’re dealing with a fixed number of total strings (100k). From a programmatic perspective, “deleting” a string is also just a matter of replacing it at the appropriate index in my data structure with an empty/null string.
As best as I can foresee, I’ll need a data structure that makes it easy to do the following:
- Add the index and string values for every non-empty string to a listbox and use the index as a ValueMember and the string as the DisplayMember.
- Quickly search the data structure for a particular index and retrieve its string value
- Quickly search the data structure for a string to see if it already exists
With those things in mind, can anyone recommend a particular data structure that lends itself to the task? I was initially thinking a dictionary with key/value pairs to hold each index/string. Then someone suggested just using an array since the total size is fixed and the array index itself could also serve as the index value for each string value.
Seeing as you have a fixed amount of items in your
Listand you require an index for each item, you need look no further than an array.You get access to
LINQfor the array too so you can meet your criteria.