简体   繁体   English

字典 <int, List<string> &gt;

[英]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. 其中counter是每次发生某事时递增+1的变量, List<string>{XXX}其中XXX是包含一些文本的字符串变量。

My question is, how do I access these strings from the list, if counter == 1? 我的问题是,如果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. FishBasketGordo解释了如何访问数据结构中的条目。 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 ...). 但你的密钥是由一个计数器变量给出的(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<,> . 因此我只使用List<>而不是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. Dictionary使用Indexer通过键访问其vallues。

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] . 您可以通过MyDic[Key][0]访问List。 While editing the list, there won't be any run time errors, however it will result in unnecessary values stored in Dictionary. 编辑列表时,不会出现任何运行时错误,但会导致在Dictionary中存储不必要的值。 So better: 好多了:

  1. assign the MyDict[Key] to new list MyDict[Key]分配给新列表
  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. 将新列表重新分配给MyDict[Key]而不是使用List as MyDict[Key]编辑Dictionary中的特定变量。

Code example: 代码示例:

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

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

MyDict[Key] = lstr; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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