C# 字典和列表
我想用c#中的字典和这样的列表结合使用C# 字典和列表,c#,list,dictionary,C#,List,Dictionary,我想用c#中的字典和这样的列表结合使用 Dictionary<int, List<int>> Dir = new Dictionary<int, List<int>>(); 它会将integer1添加为键,integer2添加为值。然后,如果我想添加到integer1键,我会这样做 Dir.Add[interger1].Add(interger3); 我还有一个问题,我有一个这样的foreach循环来显示键 foreach (KeyValue
Dictionary<int, List<int>> Dir = new Dictionary<int, List<int>>();
它会将integer1添加为键,integer2添加为值。然后,如果我想添加到integer1键,我会这样做
Dir.Add[interger1].Add(interger3);
我还有一个问题,我有一个这样的foreach循环来显示键
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
Console.WriteLine(k.Key + " "+ k.Value);
}
foreach(标签列表中的KeyValuePair k)
{
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 System.Collections.Generic.List`1[System.Int32]
6系统.集合.通用.列表'1[System.Int32]
7.System.Collections.Generic.List`1[System.Int32]
我想用嵌套的foreach,比如
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
Console.WriteLine(k.Key + " " + k.Value);
}
}
<代码>foreach(LabelList中的KeyValuePair k)
{
foreach(标签列表中的KeyValuePair k)
{
Console.WriteLine(k.Key+“”+k.Value);
}
}
但是我不确定在嵌套的foreach中放入什么来迭代列表首先,为您的key
integer1创建一个条目,除非您已经这样做了:
Dir.Add(integer1, new List<int>());
如果您正在查找其他答案,您将在其他答案中找到更多完整的代码片段。您必须先将集合添加到字典中,然后才能开始向其中添加值。下面是一个进行一次查找的解决方案(如果使用ContainsKey
,则进行两次查找)。如果缺少,它还会添加列表
public class YourClass
{
private Dictionary<int, List<int>> _dictionary = new Dictionary<int, List<int>>();
public void AddItem(int key, int value)
{
List<int> values;
if (!_dictionary.TryGetValue(key, out values))
{
values = new List<int>();
_dictionary.Add(key, values);
}
values.Add(value);
}
public IEnumerable<int> GetValues(int key)
{
List<int> values;
if (!_dictionary.TryGetValue(key, out values))
{
return new int[0];
}
return values;
}
}
公共类您的类
{
专用词典_Dictionary=新词典();
公共void附加项(int键,int值)
{
列表值;
if(!\u dictionary.TryGetValue(键,输出值))
{
值=新列表();
_添加(键、值);
}
增加(价值);
}
公共IEnumerable GetValues(int键)
{
列表值;
if(!\u dictionary.TryGetValue(键,输出值))
{
返回新整数[0];
}
返回值;
}
}
调用Dir.Add()
时,需要提供对列表
对象的引用。提供int
本身是不对的。所以你需要这样的东西:
Dir.Add(integer1, new List<int>());
Dir[integer1].Add(integer2);
Dir[integer1].Add(integer3);
if (!Dir.ContainsKey(integer1)) {
Dir.Add(integer1, new List<int>());
}
var list = Dir[integer1];
list.Add(integer2);
或者,您可以使用集合初始值设定项语法:
Dir.Add(integer1, new List<int> { integer2, integer3});
Dir.Add(integer1,新列表{integer2,integer3});
您需要添加一个列表作为值。它的工作原理如下:
Dir.Add(integer1, new List<int>());
Dir[integer1].Add(integer2);
Dir[integer1].Add(integer3);
if (!Dir.ContainsKey(integer1)) {
Dir.Add(integer1, new List<int>());
}
var list = Dir[integer1];
list.Add(integer2);
if(!Dir.ContainsKey(integer1)){
Dir.Add(integer1,newlist());
}
var list=Dir[integer1];
列表。添加(整数2);
如果要添加项目,只需使用此代码即可
(而dic
是您的字典
)
if(dic.ContainsKey(您的钥匙))
{
dic[yourKey]。添加(yourInt);
}否则{
dic[yourKey]=新列表{yourInt};
}
if
条件应为if(!\u dictionary.TryGetValue(…)
:)@Adam Houldsworth:谢谢修复=)我还添加了一个GetValues方法。当字典中不存在键时,将抛出NullReferenceException
。@MarcinJuraszek如果该键不在字典中,它将抛出KeyNotFoundException
。他询问语法。