Dictionary of Lists

c#

Solution

It doesn't matter whether you create your list as

List<string> insertReadStringHere = new List<string>();

or

List<string> foo = new List<string>();

or even

List<string> shellBeComingRoundTheMountain = new List<string>();

What's important is that once you've done

MyDictionary.Add(theInputString, shellBeComingRoundTheMountain);

you can then access that particular list via

MyDictionary[theInputString]

wherether the initial list was "called" `insertReadStringHere` or `foo` or `shellBeComingRoundTheMountain`.

You don't even need to hold the list in a named variable like this. For example,

Console.WriteLine("Input a string to create a list:");
var createListName = Console.ReadLine();
// As long as they haven't used the string before...
MyDictionary.Add(createListName, new List<string>());

Console.WriteLine("Input a string to retrieve a list:");
var retrieveListName = Console.ReadLine();
// As long as they input the same string...
List<string> retrievedList = MyDictionary[retrieveListName];

Edit: If you want a certain number of lists, use a dictionarym apping from int to string, not string to string:

int maxNumberOfLists = 5; // or whatever you read from your text file.
Dictionary<int, List<string>> myLists =
            new Dictionary<int, List<string>> (maxNumberOfLists);
for (int i = 1; i <= maxNumberOfLists; i++)
    myLists[i] = new List<string>();

Then you can access your lists as e.g.

var firstList = myLists[1];

Ordinarily I'd recommend an array, but this gives you lists from 1 to 5 rather than from 0 to 4 and it seems that's what you want.

Problem

I'm trying to build a dictionary of lists, but i'm reading in a string and need to make the lists name the string and add them to the dictionary as a key. IE read in "hello" Create the list with what was read in ``` List<string> (insert read string here ) = new List<string>(); ``` Then add that lists name as the key to a dictionary. ``` Dictionary.Add(readstring, thatlist); ``` All I can find is a hard code implementation of this. ``` Turing.Add("S", S); ``` My goal: create a Universal Turing Machine, so I read in an input from a text file that is the next step that looks like this, (q0 a) -> q1 X R. Then use the all the steps that I read in to end in a final state with this on the virtual tape "tape = XXYYZZBB" I have the the pseudo code written for this but I just cant get the dictionary to work. EDIT: Adding some more info for less confusion. Im given the start and the end state for the first 2 lines of the text file. Then im given the transitions. q0 //start state q5 //end state q0 a q1 X R //transitions Ive stripped the first two lines of input to give me 0 and 5 then have created a for loop to create a lists of each state. ``` for (int i = 0; i <= endState; i++) { List<string> i = new List<string>(); } ``` I then want to add each lists name as a key to the dictionary of lists I am creating. ``` Dictionary.Add(listname, thatlist); ``` I need help on implementing the code for the above as its giving errors.

Original source