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);
}
}
有什么我正在监督的吗?
编辑:当我让它在 aissgning 之前打印 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);
}
}
我将如何创建并访问(已排序)字典的集合?
例如,它可以是 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);
}
}
有什么我正在监督的吗? 编辑:当我让它在 aissgning 之前打印 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);
}
}