Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 列表词典_C# - Fatal编程技术网

C# 列表词典

C# 列表词典,c#,C#,我正在尝试构建一个列表字典,但我正在读取一个字符串,需要让列表命名该字符串并将其作为键添加到字典中 我读到“你好” 创建包含已读入内容的列表 List<string> (insert read string here ) = new List<string>(); 我所能找到的就是这个的硬代码实现 Turing.Add("S", S); 我的目标是:创建一个通用图灵机,所以我从一个文本文件中读取一个输入,这是下一步,看起来像这样,(q0a)->q1xr 然后在虚拟磁

我正在尝试构建一个列表字典,但我正在读取一个字符串,需要让列表命名该字符串并将其作为键添加到字典中

我读到“你好”

创建包含已读入内容的列表

List<string> (insert read string here ) = new List<string>();

我所能找到的就是这个的硬代码实现

Turing.Add("S", S);
我的目标是:创建一个通用图灵机,所以我从一个文本文件中读取一个输入,这是下一步,看起来像这样,(q0a)->q1xr

然后在虚拟磁带“tape=xxyyzbb”上使用我读到的所有步骤以最终状态结束

我已经为此编写了伪代码,但我就是无法让字典正常工作

编辑: 添加更多信息以减少混乱。 Im给出了文本文件前两行的开始和结束状态。然后给我过渡

q0//开始状态 q5//结束状态 q0aq1xr//跃迁

我去掉了前两行输入,得到了0和5,然后创建了一个for循环,创建了每个状态的列表

for (int i = 0; i <= endState; i++)
{
List<string> i = new List<string>();
}

我需要帮助来实现上面的代码,因为它给出了错误。

无论您是否将列表创建为

List<string> insertReadStringHere = new List<string>();
然后,您可以通过

MyDictionary[theInputString]
其中,初始列表被“调用”
insertReadStringHere
foo
shellBeComingRoundTheMountain

您甚至不需要像这样将列表保存在命名变量中。比如说,

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];

通常我会推荐一个数组,但这会给你从1到5的列表,而不是从0到4的列表,这似乎是你想要的。

很难理解你的要求。我已经更新了我的问题。谢谢你的帮助。所以我正在尝试创建一个每个数值1,2。。。n其中我正在读取的文本文件为我提供了最大状态。假设文本文件最多有5个状态,我想为每个数字1、2、3……创建一个列表。对于(inti=0;i@MechaMan),我添加了一些代码,用于根据编号访问列表。
List<string> foo = new List<string>();
List<string> shellBeComingRoundTheMountain = new List<string>();
MyDictionary.Add(theInputString, shellBeComingRoundTheMountain);
MyDictionary[theInputString]
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];
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>();
var firstList = myLists[1];