2012-03-12 177 views
3

好吧,所以我设法读取.txt文件...现在我试图找出将这些信息转换为二维数组的最佳方法。从txt文件创建二维数组

我的文本文件(前两个号码提供高度和宽度):

5 
5 
0,0,0,0,0 
0,0,0,0,0 
0,0,1,0,0 
0,1,1,1,0 
1,1,1,1,1 

我的C#/ XNA:

string fileContents = string.Empty; 
try 
{ 
    using (StreamReader reader = new StreamReader("Content/map.txt")) 
    { 
     fileContents = reader.ReadToEnd().ToString(); 
    } 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e.Message); 
} 

现在我下一步需要做的是确定的2尺寸维数映射数组,然后填充入口值...这是我有点卡住,并已找到各种方式我可以循环通过数据,但我不认为他们中的任何一个已经非常整洁。

我试图做的是有一个循环,以换行符分隔......然后用逗号分隔符分隔另一个循环。

这是最好的方式来做到这一点......还是有更好的选择?

+0

听起来就像你在正确的轨道上。也许尝试使用扩展方法,例如ToArray() – Tom 2012-03-12 20:45:29

+0

@JohnSaunders对不起,我的错误。 – diggersworld 2012-03-12 20:53:02

回答

0

下面的代码并不需要先排在你的示例.csv文件:

5 
5 

我更喜欢这种方式,但作为一个结果,下面的代码读取文件的两倍。取而代之的是,使用样本中的前两行进行小修改。

private int[,] LoadData(string inputFilePath) 
{ 
    int[,] data = null; 

    if (File.Exists(inputFilePath)) 
    { 
    Dictionary<string, int> counts = GetRowAndColumnCounts(inputFilePath); 

    int rowCount = counts["row_count"]; 
    int columnCount = counts["column_count"]; 

    data = new int[rowCount, columnCount]; 

    using (StreamReader sr = File.OpenText(inputFilePath)) 
    { 
     string s = ""; 
     string[] split = null; 

     for (int i = 0; (s = sr.ReadLine()) != null; i++) 
     { 
     split = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 

     for (int j = 0; j < columnCount; j++) 
     { 
      data[i, j] = int.Parse(split[j]); 
     } 
     } 
    } 
    } 
    else 
    { 
    throw new FileDoesNotExistException("Input file does not exist"); 
    } 

    return data; 
} 

private Dictionary<string, int> GetRowAndColumnCounts(string inputFilePath) 
{ 
    int rowCount = 0; 
    int columnCount = 0; 

    if (File.Exists(inputFilePath)) 
    { 
    using (StreamReader sr = File.OpenText(inputFilePath)) 
    { 
     string[] split = null; 
     int lineCount = 0; 

     for (string s = sr.ReadLine(); s != null; s = sr.ReadLine()) 
     { 
     split = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); 

     if (columnCount == 0) 
     { 
      columnCount = split.Length; 
     } 

     lineCount++; 
     } 

     rowCount = lineCount; 
    } 

    if (rowCount == 0 || columnCount == 0) 
    { 
     throw new FileEmptyException("No input data"); 
    } 
    } 
    else 
    { 
    throw new FileDoesNotExistException("Input file does not exist"); 
    } 

    Dictionary<string, int> counts = new Dictionary<string, int>(); 

    counts.Add("row_count", rowCount); 
    counts.Add("column_count", columnCount); 

    return counts; 
} 
+2

这看起来有点冗长......查看我的解决方案。 – diggersworld 2012-03-12 21:17:52

+0

是的,你做了我提到的修改。将高度和宽度放在前两行中,不需要第二个功能。但是,我不喜欢输入文件的格式。 *这是一个偏好的事情。* – JohnB 2012-03-12 21:25:24

+0

嗯......我可以理解......一旦变得很大,很难计算列数等......所以自动化会有所帮助。 – diggersworld 2012-03-12 21:40:30

0

下面是我提出的解决方案,它似乎工作。

int[,] txtmap; 
int height = 0; 
int width = 0; 
string fileContents = string.Empty; 

try 
{ 
    using (StreamReader reader = new StreamReader("Content/map.txt")) 
    { 
     fileContents = reader.ReadToEnd().ToString(); 
    } 
} 
catch (Exception e) 
{ 
    Console.WriteLine(e.Message); 
} 

string[] parts = fileContents.Split(new string[] { "\r\n" }, StringSplitOptions.None); 
for (int i = 0; i < parts.Length; i++) 
{ 
    if (i == 0) 
    { 
     // set width 
     width = Int16.Parse(parts[i]); 
    } 
    else if (i == 1) 
    { 
     // set height 
     height = Int16.Parse(parts[i]); 

     txtmap = new int[width, height]; 
    } 

    if (i > 1) 
    { 
     // loop through tiles and assign them as needed 
     string[] tiles = parts[i].Split(new string[] { "," }, StringSplitOptions.None); 
     for (int j = 0; j < tiles.Length; j++) 
     { 
      txtmap[i - 2, j] = Int16.Parse(tiles[j]); 
     } 
    } 
} 
5

它可与LINQ进行,但是这是唯一可行的,当你想(接受)一个数组的数组,int[][]而不是直上2维int[,]

int[][] data = 
    File.ReadLines(fileName) 
    .Skip(2) 
    .Select(l => l.Split(',').Select(n => int.Parse(n)).ToArray()) 
    .ToArray(); 
+0

这是什么被称为“锯齿”数组? – diggersworld 2012-03-12 21:45:59

+0

是的,但我更喜欢_array array_ – 2012-03-12 21:47:08

+0

PS:我不确定是否所有这些(ReadLines)在XNA中都可用。错过了该标签。 – 2012-03-12 21:54:02