Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the easiest way to fill gaps in a list of numbers?

I'm having some trouble explaining the question in a single line for the title, but hopefully this description will provide you with enough insight to fully understand my question:

I have a few variables in a Word document. Every variable has a Value which is a number (starting from 0 up to the number of variables). A SortedDictionary is being filled out with these variables, but it's possible that a variable has been deleted before, so there's a 'gap' so to say. Example:

5 Variables are being added to a SortedDictionary<int, string> where the first number is the int and the string corresponds to the string part of the SortedDictionary.

0 "name 1"
1 "name 2"
2 "name 3"

Now one of the variables is deleted so the dictionary is filled out like this:

0 "name 1"
2 "name 3"

Eventually all of the SortedDictionary entries are added into a list box and I'm using the first number as the index for the insertion. You can imagine that it will give an error when it tries to add an item into index 2 when index 1 doesn't exist, but index 0 does.

The way I want to solve it is when the user calls the DeleteVariable() method and a variable is taken away, all the variables should automatically update their value (number) so when the SortedDictionary gets all the variables the list box no longer gives an error because all the numbers match (no gaps).

Please advise.

like image 883
Fusyion Avatar asked Jun 29 '10 12:06

Fusyion


1 Answers

I would use a List instead of the sorted dictionary. Your number will be the index in the list. This way if you remove an item from the list (using probably list.RemoveAt(index)), all the other indices will be effectively "updated" in a way you wanted.

like image 176
Grzenio Avatar answered Oct 03 '22 01:10

Grzenio