Home > database >  How do i use a List which is inside a Dictionary
How do i use a List which is inside a Dictionary

Time:09-22

let's say I want to make a program that takes as input: {personName} {food1} {food2} ... {foodN}. Then they should be stored in a Dictionary - The Key should be the {personName}, and the Value should be a list that stores all the food[n], that is in the input. Then i want to print them like this {personName} -> {food1}, {food2}, {food3} ... {foodN}.

using System;
using System.Collections.Generic;
using System.Linq;

namespace stack1
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();  // George, Pizza, Burger
            Dictionary<string, List<string>> peopleFood = new Dictionary<string, List<string>>();
            while (input != "stop")
            {
                List<string> inputs = input.Split(" ").ToList(); // George, Pizza, Burger
                peopleFood.Add(inputs[0], inputs[1], inputs[2]); // George, Pizza, Burger
                input = Console.ReadLine(); //stop
            }
            Console.WriteLine(); // George -> Pizza, Burger
        }
    }
}

The .Add function is not working with 3 elements, and I don't know what else to use. The same goes for the printing process - I don't know how to print the output i want .

CodePudding user response:

The .Add function is not working with 3 elements

Because the .Add method is expecting two arguments. A string and a List<string>. Provide those arguments:

peopleFood.Add(inputs[0], new List<string> { inputs[1], inputs[2] });

Or perhaps:

peopleFood.Add(inputs[0], inputs.Skip(1).ToList());

I don't know how to print the output i want

I don't know how you want to format that output, but in general you can (1) loop over the dictionary and, for each element, (2) loop over the list. For example:

foreach (var item in myDictionary)
    foreach (var val in item.Value)
        Console.WriteLine(val);

CodePudding user response:

The Add function expects a Key=string as first argument and a Value=list of strings as second argument. It will not magically interpret any other arguments you give it.

input.Split(" ") will generate a string[]. You can use that directly with (ranged) indexing.

inputs[0] would be the first element and inputs[1..].ToList() will make you a new list of the rest. There you have your arguments.

Note, you will have to check yourself if the original input actually consist of enough strings.

CodePudding user response:

Both the previous responders, David and JHBonarius, gave good answers and warnings about why your attempt to add values to the dictionary wasn't working. So I won't repeat that. There currently aren't any answers about how to get the output in the format you want (name -> food list). You could do something like this after your while loop to achieve that:

Console.Write(String.Join("\n"), peopleFood.Select(kvp => kvp.Key   " -> "   String.Join(", ", kvp.Value))));
Console.ReadLine();
  •  Tags:  
  • c#
  • Related