在For循环中更改变量名称以反映迭代器的值?

arm*_*ive 0 c# for-loop streamreader

我试图从不同的文本文件加载到内存中的文本.它们都是单词,它们都按各自文本文件中单词的长度分组(例如words3.txt,words4.txt ...)

我正在使用StreamReader文件,并且由于语法,我相当肯定我可以迭代它正在使用哪个文件,如果我在for循环内执行它.我不明白为什么我应该有12个不同的using陈述.

String[] words3 = new String[2000];

for (int i = 0; i < 12; i++)
{
    using (StreamReader sr = new StreamReader(path + "words" + (i+3) + ".txt"))
        {
            String strTemp = sr.ReadLine();
            words3 = strTemp.Split(' '); //My current logic fails here
        }
}
Run Code Online (Sandbox Code Playgroud)

我想迭代我的不同单词数组(单词3,单词4 ......单词15),但我自然而然地遇到了一个问题,我正在存储这些数组的名称.它保持不变,所以我只是覆盖它12次.在VB.NET我可以将迭代器变量连接到数组名称,就像这样(或类似于此的东西):

words & (i+3) = strTemp.Split(' ');
Run Code Online (Sandbox Code Playgroud)

这显然不会像我描述的那样在C#中起作用.解决这个问题的最佳方法是什么?我可以将数组放入一个更大的数组中并以某种方式迭代它们吗?在文本文件中,单词不存储在单独的行上,它们由单个空格分隔.为了节省时间,当我去查看用户的单词是否包含在我的"词典"中时,我只想在包含具有适当字母数的单词的数组中搜索匹配项.

Dav*_*ych 5

为什么不创建一个List数组?

List<string[]> stringList = new List<string[]>();
for (int i = 0; i < 12; i++)
{
    using (StreamReader sr = new StreamReader(path + "words" + (i+3) + ".txt"))
        {
            String strTemp = sr.ReadLine();
            stringList.Add(strTemp.Split(' '));
        }
}
Run Code Online (Sandbox Code Playgroud)


Llo*_*oyd 5

使用类似字典的东西:

Dictionary<int,string[]> word_dict = new Dictionary<int,string[]>();

    for (int i = 0; i < 12; i++)
    {
        using (StreamReader sr = new StreamReader(path + "words" + (i+3) + ".txt"))
            {
                String strTemp = sr.ReadLine();
                string[] words = strTemp.Split(' ');

                word_dict.Add(i + 3,words);
            }
    }
Run Code Online (Sandbox Code Playgroud)

然后把话说回来:

string[] words3 = word_dict[3];
Run Code Online (Sandbox Code Playgroud)