Reputation: 521
I want to use the dictionary in c# combined with a list like this
Dictionary<int, List<int>> Dir = new Dictionary<int, List<int>>();
but I am having trouble with the syntax for adding the keys and values. I thought if I just did something like
Dir.Add(integer1, integer2);
it would add integer1 was the key, and integer2 as the value. Then if I wanted to add to the integer1 key I would do
Dir.Add[interger1].Add(interger3);
I had one more question, I had a foreach loop like this to display the keys
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
Console.WriteLine(k.Key + " "+ k.Value);
}
it displays the keys I expected up to 7, but it doesn't display the value's I wanted it to show, it just displays
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 System.Collections.Generic.List`1[System.Int32]
7 System.Collections.Generic.List`1[System.Int32]
I have any idea to use a nested foreach like
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
foreach (KeyValuePair<int, List<int>> k in labelsList)
{
Console.WriteLine(k.Key + " " + k.Value);
}
}
but I am unsure of what to put inside the nested foreach to iterate through the list
Upvotes: 1
Views: 366
Reputation: 10895
If you want to add an item, simply use this code
(whereas dic
is your Dictionary<Int32, List<Int32>>
)
if (dic.ContainsKey(yourKey))
{
dic[yourKey].Add(yourInt);
} else {
dic[yourKey] = new List<int> { yourInt };
}
Upvotes: 0
Reputation: 6717
First, create an entry for your key integer1
, unless you have already done so:
Dir.Add(integer1, new List<int>());
Then, find the right dictionary entry, then add to its value (in this case, your list):
Dir[integer1].Add(integer2);
You will find more entire code snippets in the other answers, if that is what you are looking for.
Upvotes: 0
Reputation: 101130
You have to add the collection into the dictionary before you can start adding values to it. Here is a solution which make one lookup (compared to two if ContainsKey
is used). It also adds the list if it's missing.
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;
}
}
Upvotes: 2
Reputation: 11166
You need to add a List<int>
as value. It'll work like this:
if (!Dir.ContainsKey(integer1)) {
Dir.Add(integer1, new List<int>());
}
var list = Dir[integer1];
list.Add(integer2);
Upvotes: 0
Reputation: 415600
When you call Dir.Add()
, you need to provide a reference to a List<int>
object. Providing an int
itself is not right. So you need something like this:
Dir.Add(integer1, new List<int>());
Then you can update that entry like this:
Dir[integer1].Add(integer2);
Dir[integer1].Add(integer3);
Alternatively, you can use the collection initializer syntax:
Dir.Add(integer1, new List<int> { integer2, integer3});
Upvotes: 1