【问题标题】:Dictionary<int, List<string>>字典<int, List<字符串>>
【发布时间】:2013-06-11 02:20:43
【问题描述】:

我有这样的事情:

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

然后,我用一些变量填充它,例如:

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

其中counter 是一个变量,每次发生某事时都会增加+1,List&lt;string&gt;{XXX} 其中XXX 是包含一些文本的字符串变量。

我的问题是,如果 counter == 1,我如何从列表中访问这些字符串?

【问题讨论】:

  • 您将在 msdn 参考中找到一些示例:msdn.microsoft.com/en-us/library/xfhwa508.aspx(或在 Google 上进行简单查询:C# 字典示例)。
  • Power Collections 包括库,其中包括一个多字典。
  • “smth”是什么意思?请考虑使用速记而不是 1337 短信语言。
  • 为什么 fileList 甚至是字典?如果你在一个整数键上按顺序填充,List&lt;List&lt;string&gt;&gt; 不是更合适吗?

标签: c# list dictionary


【解决方案1】:

您可以像平常一样访问字典和列表中的数据。请记住,首先访问字典中的值,这将返回一个列表。然后,访问列表中的项目。

例如,你可以索引到字典中,它返回一个列表,然后索引到列表中:

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

【讨论】:

  • 和 fileList[1][] 的长度?
  • fileList[1] 的值是一个列表。您可以在列表变量上调用的任何内容,都可以对其进行调用。因此,要获得长度,只需执行fileList[1].Length
  • ^^^ 哈...调试 5 年前的评论 ^^^ List&lt;T&gt; 没有 Length,但确实有 Count
【解决方案2】:

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

字典(基于哈希表)允许快速访问任意键。但是您的键是由一个计数器变量(计数器 = 0、1、2、3、4 ...)给出的。访问此类键的最快方法是简单地使用数组或列表的索引。因此,我只使用List&lt;&gt; 而不是Dictionary&lt;,&gt;

此外,您的列表似乎没有列出匿名值,而是列出了具有非常具体和不同含义的值。 IE。日期与名称不同。在这种情况下,我将创建一个类来存储这些值并允许单独访问各个值。

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

现在您可以像这样创建一个列表:

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

你可以访问这样的信息

decimal version = fileList[5].Version;

如果键不从零开始,只需减去起始值:

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

【讨论】:

    【解决方案3】:

    字典使用Indexer 通过键访问其值。

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

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

    【讨论】:

      【解决方案4】:
      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);
          }
      }
      

      【讨论】:

        【解决方案5】:

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

        1. MyDict[Key] 分配给新列表
        2. 编辑新列表,然后
        3. 将新列表重新分配给MyDict[Key],而不是编辑 字典中以列表为值的特定变量。

        代码示例:

        List<string> lstr = new List<string(MyDict[Key]); 
        
        lstr[0] = "new Values";
        lstr[1] = "new Value 2";
        
        MyDict[Key] = lstr; 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-08-11
          相关资源
          最近更新 更多