将数据从文本文件加载到二维数组中?

将数据从文本文件加载到二维数组中?

问题描述:

我在控制台应用程序中使用 C#.

I am using C# in a console application.

我需要从文本文件加载数据并将其加载到二维数组中.

I need to load data from a text file and load it into a 2d array.

这是我尝试过的,但是当我尝试打印返回的内容时,没有打印任何内容.

This is what I tried, but when I try to print out the contents of what gets returned nothing gets printed.

public static int[,] LoadMap()
{
    const string path = @"1.txt";

    string[] fileLines = File.ReadAllLines(path);
    int[,] map = new int[fileLines.Length, 15];
    string line;
    for (int i = 0; i < fileLines.Length; ++i)
    {
        line = fileLines[i];
        for (int j = 0; j < line.Length; ++j)
        {
            map[i, j] = (int)(line[j] - '0');
        }
    }

    return map;
}

但是当我像这样对数据进行硬编码时,一切都会完美显示.

But when I hardcode the data like that, then everything gets displayed perfectly.

private static int[,] Map = new int[MapX, MapY]
{
            { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
            { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
};

文本文件中的数据如下所示:

The data in the text file looks like that :

0,0,0,1,1,1,1,1,1,1,1,1,1,1,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
0,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,0,0,0,0,0,0,0,0,0,0,0,0,0,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

无论您是修复我尝试过的问题还是提出完全不同的建议,我们都将不胜感激,谢谢.

Any help will be appreciated whether you fix what I tried or propose something completely different, thanks.

您可以使用 LINQ 来解析行:

You could use LINQ to parse the lines:

var lines = File.ReadAllLines(path);
int[,] map = new int[fileLines.Length, 25];
for (int i = 0; i < fileLines.Length; ++i)
{
    var data = lines[i].Split(',').Select(c => Convert.ToInt32(c)).ToList();
    for(int j =0; j<25; ++j)
       map[i,j] = data[j];
}
return map;

如果您可以使用锯齿状数组而不是二维数组,这会变得更简单:

If you could use a jagged array instead of a 2D array, this becomes simpler:

public static int[][] LoadMap()
{
    return File.ReadLines(path)
             .Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
             .ToArray();
}