Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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#_List - Fatal编程技术网

C# 向列表中添加字符串<;字符串>;列表内部<;列表<;字符串>&燃气轮机;

C# 向列表中添加字符串<;字符串>;列表内部<;列表<;字符串>&燃气轮机;,c#,list,C#,List,我试图在for&while循环中向list>中的列表添加一个string对象,试图使用var i作为我希望使用的列表对象 这是本课程的代码,任何关于我做错了什么的帮助都将不胜感激:) 公共类GenClass { 专用静态int_genCount; 私有静态bool\u文件加载的策略; 私有静态列表_nounSetOne=新列表(); 私有静态列表_nounSetTwo=新列表(); 私有静态列表_toLoad=new List(); 私有字符串_emotionMidTrim=“”; public

我试图在for&while循环中向list>中的列表添加一个string对象,试图使用var i作为我希望使用的列表对象

这是本课程的代码,任何关于我做错了什么的帮助都将不胜感激:)

公共类GenClass
{
专用静态int_genCount;
私有静态bool\u文件加载的策略;
私有静态列表_nounSetOne=新列表();
私有静态列表_nounSetTwo=新列表();
私有静态列表_toLoad=new List();
私有字符串_emotionMidTrim=“”;
public const string FileOne=“NounSetOne.txt”;
public const string FileTwo=“NounSetTwo.txt”;
公共类()
{
while(_filesLoadedToLists==false)
{
文本列表(文件一、文件二);
_filesLoadedToLists=true;
}
_genCount++;
}
问题在于这一部分的课程

    public void TextToList(string fileOne, string fileTwo)
    {
        List<string> filesToRead = new List<string>();

        filesToRead.Add(fileOne); // Add the text files to read to a list
        filesToRead.Add(fileTwo); // Add the text files to read to a list

        _toLoad.Add(_nounSetOne); // Add a list of words to this list
        _toLoad.Add(_nounSetTwo); // Add a list of words to this list

        for (int i = 0; i <= filesToRead.Count; i++)
        {
             using (var reader = new StreamReader(filesToRead[i]))
             {
                 string line;
                 while ((line = reader.ReadLine()) != null)
                 {
                     _toLoad[i.Add(line)]; // the error is here
                 }
             }
        }
public void TextToList(字符串fileOne,字符串fileTwo)
{
List filesToRead=新列表();
filesToRead.Add(fileOne);//将要读取的文本文件添加到列表中
filesToRead.Add(fileTwo);//将要读取的文本文件添加到列表中
_toLoad.Add(_noungsetone);//将单词列表添加到此列表中
_toLoad.Add(_nounSetTwo);//将单词列表添加到此列表中

对于(inti=0;i您是正确的,有了错误,您需要理解

列表
将采用
列表
而不是字符串。

试试这样的东西

List<string> listOfString = new List<string>;
for (int i = 0; i <= filesToRead.Count; i++)
{
      using (var reader = new StreamReader(filesToRead[i]))
      {
           string line;
           while ((line = reader.ReadLine()) != null)
           {
             listOfString.add(line);
           }
      }
}

尝试使用File.ReadAllLines()。将for循环替换为:

foreach(var file in filesToRead) {
  _toLoad.Add(File.ReadAllLines(file).ToList());
}

您可以使用LINQ大大减少这种情况:

List<string> filesToRead = new List<string> {"NounSetOne.txt", "NounSetTwo.txt"};
List<List<string>> _toLoad = new List<List<string>>();
_toLoad.AddRange(filesToRead.Select(f => File.ReadAllLines (f).ToList() ));
List filesToRead=newlist{“NounSetOne.txt”、“NounSetTwo.txt”};
列表_toLoad=新列表();
_toLoad.AddRange(filesToRead.Select(f=>File.ReadAllLines(f.ToList());

请注意,文件名没有额外的变量(如果FileOne/FileTwo的唯一目的是添加到列表中,为什么要使用它们呢?),我们让AddRange负责为我们自动创建
列表(int i=0;i
i
是一个整数,因此您不能对其调用
List
方法。请尝试
\u-toLoad[i]。添加(行)
,其中
i
是索引。谢谢maxbeaudoin!这就是我需要的:)我对编程非常陌生,但仍在尝试降低逻辑:P@DataHead我不太清楚你的意思是什么?\u toLoad是一个List.File.ReadAllLines(File).ToList()是一个List,所以它应该可以添加。
for (int i = 0; i <= filesToRead.Count; i++)
    {
         using (var reader = new StreamReader(filesToRead[i]))
         {
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 _toLoad[i].Add(line);
             }
         }
    }
List<string> filesToRead = new List<string> {"NounSetOne.txt", "NounSetTwo.txt"};
List<List<string>> _toLoad = new List<List<string>>();
_toLoad.AddRange(filesToRead.Select(f => File.ReadAllLines (f).ToList() ));
for (int i = 0; i <= filesToRead.Count; i++)
    {
         using (var reader = new StreamReader(filesToRead[i]))
         {
             string line;
             while ((line = reader.ReadLine()) != null)
             {
                 _toLoad[i].Add(line);
             }
         }
    }