C# 字典<整数,列表<字符串>>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17108399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:37:27  来源:igfitidea点击:

Dictionary<int, List<string>>

c#listdictionary

提问by DejmiJohn

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 counteris a variable that increment +1 each time something happens, List<string>{XXX}where XXXare 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,我如何从列表中访问这些字符串?

采纳答案by FishBasketGordo

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.

回答by Mehmet Ata?

Dictionary uses Indexerto 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.

然后你可以对列表做任何事情。

回答by Olivier Jacot-Descombes

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

FishBasketGordo 解释了如何访问数据结构中的条目。我只在这里补充一些想法:

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<,>.

字典(基于哈希表)允许快速访问任意键。但是您的键是由计数器变量给出的(计数器 = 0, 1, 2, 3, 4 ...)。访问这些键的最快方法是简单地使用数组或列表的索引。因此,我只会使用 aList<>而不是 a Dictionary<,>

Furthermore, your list seems not to list anonymous values but rather values having very specific and distinct meanings. I.e. 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;

回答by Fijo Francis T

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);
    }
}

回答by Rishi

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:

您可以通过 访问该列表MyDic[Key][0]。编辑列表时,不会有任何运行时错误,但会导致在 Dictionary 中存储不必要的值。所以更好:

  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.
  1. 分配MyDict[Key]到新列表
  2. 编辑新列表,然后
  3. 将新列表重新分配给MyDict[Key]而不是使用列表作为值编辑字典中的特定变量。

Code example:

代码示例:

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

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

MyDict[Key] = lstr;