[英]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.