2013-10-25 160 views
0

我在控制檯應用程序中使用C#。將數據從文本文件加載到二維數組中?

我需要從文本文件加載數據並將其加載到2d數組中。

這就是我所嘗試的,但是當我嘗試打印出什麼被返回的內容時,什麼都得不到打印。

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 

任何幫助將不勝感激。

回答

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(); 
} 
+0

謝謝里德,我用的第一個代碼,你提出的,現在改變了它一下它完美的作品! – Marc

0

如果您的文本文件有逗號分離的值,替換這一行:

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

隨着:

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

這是假設您的值將始終只有1個字符長。

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

當數據從文本文件中傳入時。如果用逗號分隔,可以使用string.split將其分開。然後你將你得到的數據加載到一個數組中,並像通常做一個數組一樣訪問它。 象下面這樣:

string[] lines = System.IO.File.ReadAllLines(@"path"); 
    foreach (string line in lines) 
    { 
     string[] first= line.Split(comma); 
    }