简体   繁体   中英

Dictionary<int, List<string>>

I have something like this:

Dictionary<int, List<string>> fileList = new Dictionary<int, List<string>>();

and then, I fill it with some variables, for example:

fileList.Add(
    counter, 
    new List<string> { 
        OFD.SafeFileName, 
        OFD.FileName, 
        VERSION, NAME      , DATE  , 
        BOX    , SERIAL_NUM, SERIES,  
        POINT  , NOTE      , VARIANT
    }
);

Where counter is a variable that increment +1 each time something happens, List<string>{XXX} where XXX are string variables that holds some text.

My question is, how do I access these strings from the list, if counter == 1?

You can access the data in the dictionary and lists just like normal. Remember, access a value in the dictionary first, which will return a list. Then, access the items in the list.

For example, you can index into the dictionary, which returns a list, and then index into the list:

         ------ Returns a list from the dictionary
         |  --- Returns an item from the list
         |  |
         v  v
fileList[0][0] // First item in the first list
fileList[1][0] // First item in the second list
fileList[1][1] // Second item in the second list
// etc.

FishBasketGordo explains how you can access entries in your data structure. I will only add some thoughts here:

Dictionaries (based on hash tables) allow fast access to arbitrary keys. But your keys are given by a counter variable (counter = 0, 1, 2, 3, 4 ...). The fastest way to access such keys is to simply use the index of an array or of a list. Therefore I would just use a List<> instead of a Dictionary<,> .

Furthermore, your list seems not to list anonymous values but rather values having very specific and distinct meanings. Ie a date is not the same as a name. In this case I would create a class that stores these values and that allows an individual access to individual values.

public class FileInformation 
{
    public string SafeFileName { get; set; }
    public string FileName { get; set; }
    public decimal Version { get; set; }
    public string Name { get; set; }
    public DateTime Date { get; set; }
    ...
}

Now you can create a list like this:

var fileList = new List<FileInformation>();
fileList.Add(
    new FileInformation {
        SafeFileName = "MyDocument.txt",
        FileName = "MyDocument.txt",
        Version = 1.2,
        ...
    }
}

And you can access the information like this

decimal version = fileList[5].Version;

If the keys don't start at zero, just subtract the starting value:

int firstKey = 100;
int requestedKey = 117;
decimal version = fileList[requestedKey - firstKey].Version;

Dictionary uses Indexer to access its vallues via key.

List<string> items = fileList[counter];
var str0 = items[0];
var str1 = items[1];

Then you can do anything with the list.

Dictionary<int, List<string>> fileList = new Dictionary<int, List<string>>();
fileList.Add(101, new List<string> { "fijo", "Frigy" });
fileList.Add(102, new List<string> { "lijo", "liji" });
fileList.Add(103, new List<string> { "vimal", "vilma" });

for (int Key = 101; Key < 104; Key++)
{
    for (int ListIndex = 0; ListIndex < fileList[Key].Count; ListIndex++)
    {
       Console.WriteLine(fileList[Key][ListIndex] as string);
    }
}

You can access the List through MyDic[Key][0] . While editing the list, there won't be any run time errors, however it will result in unnecessary values stored in Dictionary. So better:

  1. assign the MyDict[Key] to new list
  2. edit the new list and then
  3. reassign the new list to MyDict[Key] rather than editing a particular variable in the Dictionary with List as Values.

Code example:

List<string> lstr = new List<string(MyDict[Key]); 

lstr[0] = "new Values";
lstr[1] = "new Value 2";

MyDict[Key] = lstr; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM