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_For Loop_Multidimensional Array - Fatal编程技术网

C# 将文本文件添加到列表中,然后添加到二维数组中

C# 将文本文件添加到列表中,然后添加到二维数组中,c#,list,for-loop,multidimensional-array,C#,List,For Loop,Multidimensional Array,我有一个文本文件,其中包含以下内容:没有引号和空白 空位 空位 空位 空位 我想逐行将整个文件添加到列表中: FileStream FS = new FileStream(@"FilePath",FileMode.Open); StreamReader SR = new StreamReader(FS); List<string> MapLine = new List<string>(); foreach (var s in SR.ReadLine()) { M

我有一个文本文件,其中包含以下内容:没有引号和空白

空位 空位 空位 空位 我想逐行将整个文件添加到列表中:

FileStream FS = new FileStream(@"FilePath",FileMode.Open);
StreamReader SR = new StreamReader(FS);
List<string> MapLine = new List<string>();

foreach (var s in SR.ReadLine())
{
    MapLine.Add(s.ToString());                   
}

foreach (var x in MapLine)
{
    Console.Write(x);
}
我的问题来了:我想把它添加到一个二维数组中。我试过:

string[,] TwoDimentionalArray = new string[100, 100];

for (int i = 0; i < MapLine.Count; i++)
{
    for (int j = 0; j < MapLine.Count; j++)
    {
        TwoDimentionalArray[j, i] = MapLine[j].Split('\n').ToString();
    }
}

我还是C的新手,请提供任何帮助,我们将不胜感激。

目前,您正在检查文件的所有行,并且对于文件的每一行,您将再次检查文件的所有行,以便在\n上拆分它们,这是通过将它们放在MapLine中完成的

如果您想要一个行数组中的每一个字符'd',并且该字符再次出现在一个数组中,它应该大致如下所示:

string[,] TwoDimentionalArray = new string[100, 100];

for (int i = 0; i < MapLine.Count; i++)
{
     for (int j = 0; j < MapLine[i].length(); j++)
     {
          TwoDimentionalArray[i, j] = MapLine[i].SubString(j,j);
     }
}
我这样做没有测试,所以它可能是错误的。关键是你需要先遍历每一行,然后遍历该行中的每个字母。从那里,你可以使用

另外,我希望我正确理解了您的问题。

您可以尝试以下方法:

        // File.ReadAllLines method serves exactly the purpose you need
        List<string> lines = File.ReadAllLines(@"Data.txt").ToList();

        // lines.Max(line => line.Length) is used to find out the length of the longest line read from the file
        string[,] twoDim = new string[lines.Count, lines.Max(line => line.Length)];

        for(int lineIndex = 0; lineIndex < lines.Count; lineIndex++)
            for(int charIndex = 0; charIndex < lines[lineIndex].Length; charIndex++)
                twoDim[lineIndex,charIndex] = lines[lineIndex][charIndex].ToString();

        for (int lineIndex = 0; lineIndex < lines.Count; lineIndex++)
        {
            for (int charIndex = 0; charIndex < lines[lineIndex].Length; charIndex++)
                Console.Write(twoDim[lineIndex, charIndex]);

            Console.WriteLine();
        }

        Console.ReadKey();

这将把文件内容的每个字符保存到二维数组中它自己的位置。出于这个目的,可能还使用了char[,]。

您到底想得到什么?似乎您的代码迭代次数太多了。张贴一些你想看到的结果的例子。为什么你需要标注尺寸?你能把每一行都当作一个字符串并包含在一个列表中吗?嗨,我只想在二维数组中显示文本文件的数据并显示数组你想把列表添加到字符串[,]中吗?字符串[,]中的元素应该是什么?for循环可以工作,但它不会向数组中添加任何内容。对不起,伙计们,我还是个新手C:轰!!!!!!!我的天啊,非常感谢你的工作非常完美!!!!!我太棒了,非常感谢你太好了,我很乐意帮忙:如果你觉得这个答案很有帮助,你可以把它标记为正确的。