0

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

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

这是我尝试过的,但是当我尝试打印返回的内容时,什么都没有打印出来。

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;
}

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

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}
};

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

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

无论您解决我尝试过的问题还是提出完全不同的建议,任何帮助都将不胜感激,谢谢。

4

4 回答 4

3
 string[] fileLines = File.ReadAllLines(path);
        int[,] map = new int[fileLines.Length,fileLines[0].Split(',').Length];
        for (int i = 0; i < fileLines.Length; ++i)
        {
            string line = fileLines[i];
            for (int j = 0; j < map.GetLength(1); ++j)
            {
                string[] split = line.Split(',');
                map[i, j] = Convert.ToInt32(split[j]);
            }
        }
        return map;
    }
于 2013-10-25T19:18:38.250 回答
0

您可以使用 LINQ 来解析这些行:

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;

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

public static int[][] LoadMap()
{
    return File.ReadLines(path)
             .Select(l => l.Split(',').Select(Convert.ToInt32).ToArray())
             .ToArray();
}
于 2013-10-25T17:29:42.777 回答
0

如果您的文本文件有逗号分隔值,请替换此行:

for (int j = 0; j < line.Length; ++j)

和:

for (int j = 0; j < line.Length; j += 2)

这是假设您的值始终只有 1 个字符长。

于 2013-10-25T17:31:32.853 回答
0

当数据来自文本文件时。如果它用逗号分隔,您可以使用 string.split 将其分隔。然后你把你得到的东西加载到一个数组中,并像往常一样访问它。如下所示:

  string[] lines = System.IO.File.ReadAllLines(@"path");
  foreach (string line in lines)
  {  
      string[] first= line.Split(comma);
  }
于 2018-03-07T16:58:08.047 回答