C# 将项目添加到字典中的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14991688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
adding items to a list in a dictionary
提问by user2093348
I'm trying to put values into a dictionary dependent on the key... For example, if in a list of keys at the index 0 there is a letter "a". I want to add the val with index 0 to a list inside of a dictionary with the key "a" ( dictionary (key is "a" at index 0 , val at index 0) ... dictionary (key is "b" at index 2 , val at index 2))
我正在尝试将值放入依赖于键的字典中......例如,如果在索引 0 处的键列表中有一个字母“a”。我想将索引为 0 的 val 添加到字典内的列表中,键为“a”(字典(字典(索引为 0 处的关键字为“a”,索引为 0 处为 val)...字典(关键字为“b”在索引 2 , val 在索引 2))
I'm expecting an output like this:
我期待这样的输出:
in listview lv1: 1,2,4 in listview lv2: 3,5
what I'm getting is 3,4,5 in both listviews
在列表视图 lv1 中:1,2,4 在列表视图 lv2 中:3,5
我在两个列表视图中得到的是 3,4,5
List<string> key = new List<string>();
List<long> val = new List<long>();
List<long> tempList = new List<long>();
Dictionary<string, List<long>> testList = new Dictionary<string, List<long>>();
key.Add("a");
key.Add("a");
key.Add("b");
key.Add("a");
key.Add("b");
val.Add(1);
val.Add(2);
val.Add(3);
val.Add(4);
val.Add(5);
for (int index = 0; index < 5; index++)
{
if (testList.ContainsKey(key[index]))
{
testList[key[index]].Add(val[index]);
}
else
{
tempList.Clear();
tempList.Add(val[index]);
testList.Add(key[index], tempList);
}
}
lv1.ItemsSource = testList["a"];
lv2.ItemsSource = testList["b"];
Solution:
解决方案:
replace the else code section with :
将 else 代码部分替换为:
testList.Add(key[index], new List { val[index] });
testList.Add(key[index], new List { val[index] });
thx everybody for your help =)
谢谢大家的帮助 =)
采纳答案by Steve
You are using the same list for both keys in the Dictionary
您对字典中的两个键使用相同的列表
for (int index = 0; index < 5; index++)
{
if (testList.ContainsKey(key[index]))
{
testList[k].Add(val[index]);
}
else
{
testList.Add(key[index], new List<long>{val[index]});
}
}
Just create one new List(Of Long) when the key doesn't exists then add the long value to it
只需在键不存在时创建一个新的 List(Of Long) 然后将 long 值添加到它
回答by Greg B
回答by lahsrah
Replace else with:
将其他替换为:
else
{
tempList.Clear();
tempList.Add(val[index]);
testList.Add(key[index], new List<long>(tempList));
}
The problem is, you are adding a reference to TempList to both keys, it is the same reference so it gets replaced in the first one.
问题是,您正在向两个键添加对 TempList 的引用,它是相同的引用,因此它在第一个中被替换。
I am creating a new list so it doesn't get replaced: new List<long>(tempList)
我正在创建一个新列表,因此它不会被替换: new List<long>(tempList)
回答by Jim Mischel
Get rid of the tempList
and replace your else
clause with:
删除tempList
并替换您的else
条款:
testList.Add(key[index], new List<long> { val[index] });
And don't use Contains
. TryGetValue
is much better:
并且不要使用Contains
. TryGetValue
好多了:
for (int index = 0; index < 5; index++)
{
int k = key[index];
int v = val[index];
List<long> items;
if (testList.TryGetValue(k, out items))
{
items.Add(v);
}
else
{
testList.Add(k, new List<long> { v });
}
}
回答by Tony Hopkinson
I'm not completely sure what you are trying to do here, but I guarantee you didn't want the same list in every dictionary entry.
我不完全确定您要在这里做什么,但我保证您不希望每个字典条目中都使用相同的列表。
templist is your problem swap templist.Clear()
for templist = new List<Long>()
templist是你的问题交换templist.Clear()
了templist = new List<Long>()
Or go for
或者去
for (int index = 0; index < 5; index++)
{
if (!testList.ContainsKey(key[Index]))
{
testList.Add(key[Index], new List<Long>());
}
testList[key[index]].Add(val[index]);
}