繁体   English   中英

c# 词典收藏

[英]c# Collection of Dictionary

我将如何创建然后访问(已排序)字典的集合? 例如,它可以是Series<SortedDictionary<double, MyClass>>Dictionary<int, SortedDictionary<double, MyClass>>

Series[0] = sortedDict;
Dict.Add(myInt, sortedDict);

出于某种原因,当这样做时,字典是空的,当使用循环访问值时,系列似乎也是空的。

for (int i = 0; i < Dict.Count; i++)
{
   SortedDictionary<double, MyClass> sortedDictPair = Dict.ElementAt(i).Value;
   foreach (var item in sortedDictPair)
   {
        Print(item.Key);
        MyClass myClass= item.Value;
        Print(myClass.classMember);
    }
}

SortedDictionary<double, MyClass> sortedDictPair = Series[0];
for (int j = 0; j < sortedDictPair.Count; j++)
    {
        MyClass myClass = sortedDictPair.ElementAt(j).Value;
        Print(myClass.classMember);
    }
 }

有什么我正在监督的吗? 编辑:当我让它在分配之前打印 sortedDict 时,它充满了条目。

您可以在两个 foreach 循环中尝试它们吗?

Dictionary<int, SortedDictionary<double, MyClass>> dictionary = new Dictionary<int, SortedDictionary<double, MyClass>>()
    {
        { 1, new SortedDictionary<double,MyClass>(){ {2, new MyClass{ Score=1 } }, { 3, new MyClass { Score = 1 } } } },
        { 2, new SortedDictionary<double,MyClass>()}
    };
    foreach (var item1 in dictionary)
    {
        foreach (var item2 in item1.Value)
        {
            Console.WriteLine(item2.Key);
            MyClass myClass = item2.Value;
            Console.WriteLine(myClass.Score);
        }
    }

查看

暂无
暂无

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

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