C# - 在控制台中基于 int 创建多个列表

C# - Creating multiple lists based on int in Console

我想根据给定的大小创建多个列表。想象一下它可能看起来像这样:

int Size = int.Parse(Console.ReadLine());
for (int i = 0; i < Size; i++)
{
    List<string> ListName + i = new List<string>();
}

例如,如果 size = 5 我会得到 5 个列表:

ListName0
ListName1
ListName2
ListName3
ListName4

为循环外的列表创建一个容器:

int Size = int.Parse(Console.ReadLine());

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

for (int i = 0; i < Size; i++)
{
    listContainer.Add(new List<string>());
}

您可以通过容器对象的索引访问它们。例如 listContainer[0] 将是容器中的第一个 list<string>

这是访问其中一个列表然后访问所述列表中的值的示例:

int Size = int.Parse(Console.ReadLine());

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

for (int i = 0; i < Size; i++)
{
    listContainer.Add(new List<string>());
}

listContainer[0].Add("Hi");
Console.WriteLine(listContainer[0][0]);

常用的方法是使用字典

    var list = new Dictionary<string, List<string>>();
    int size = int.Parse(Console.ReadLine());
    for (int i = 0; i < size; i++)
            list["Name"+i.ToString()] = new List<string>();

如何使用

    list["Name1"].Add( "hello world");