2012-03-12 229 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