2014-04-08 77 views
1

我正在做的是從文本文件中讀入內容,然後將內容存儲在二維數組中,但是我的數組在每一行和一列處都被關閉。也許這很簡單,我只是沒有看到它?從文本文件中讀取二維數組

我的文本文件的內容:

0007,0007,0007,0007,0007, 
0007,0007,0007,0007,0007, 
0007,0007,0007,0007,0007, 
0007,0007,0007,1707,0007, 
0007,0007,0007,0007,0401 

當返回我的數組,價值17位於[3,3] ...應該是[4,4]。以下是我的代碼。我已經花了很多時間在這方面,有人可以幫忙嗎?

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

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

     returnArray = 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++) 
       { 
        returnArray[i, j] = int.Parse(split[j].Substring(0,2)); 
       } 
      } 
     } 
    } 
    else 
    { 
     // throw new FileDoesNotExistException("Input file does not exist"); 
    } 
    return returnArray; 
} 
+7

從我看到的地方看,結果很好。數組索引從0開始,而不是1,所以在第4行和第4列上的'1707'實際上在索引3,3。 –

+0

1基礎指標是魔鬼。每天晚上和早上重複一遍。 – TyCobb

+0

當然我知道數組是基於零的。但是,謝謝你確認代碼是正確的。事實證明,這並不是我遇到的問題的一部分。現在我很好,我跟蹤了真正的問題。謝謝。 – user3511543

回答