我想用C#中的字典與列表相結合這樣字典和列表
Dictionary<int, List<int>> Dir = new Dictionary<int, List<int>>();
,但我有添加鍵和值的語法的麻煩。我想如果我只是做了一些像
Dir.Add(integer1, integer2);
它會添加integer1是關鍵,整數2作爲值。然後,如果我想添加到整數1鍵,我會做
Dir.Add[interger1].Add(interger3);
我有一個問題,我有一個foreach循環是這樣顯示的按鍵
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
Console.WriteLine(k.Key + " "+ k.Value);
}
它顯示我的預期的關鍵至多7個,但不顯示該值的我希望它表明,它只是顯示
1 System.Collections.Generic.List`1 [System.Int32]
2 System.Collections.Generic .List`1 [System.Int32]
3 System.Collections.Generic.List`1 [System.Int32]
4 System.Collections.Generic.List`1 [System.Int32]
5系統。 Collections.Generic.List`1 [System.Int32]
6 System.Collections.Generic.List`1 [System.Int32]
7 System.Collections.Generic.List`1 [System.Int32]
我h AVE任何主意,用一個嵌套的foreach像
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
Console.WriteLine(k.Key + " " + k.Value);
}
}
但我不確定該怎麼把嵌套的foreach內通過列表
非常感謝。 –