我正在做的是从文本文件中读取,然后将内容存储在2D数组中,但是我的数组在每行和每列都是一个。也许这很简单,我只是没有看到它?
我的文字档案的内容:
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;
}
答案 0 :(得分:1)
数组索引从0开始。因此索引3是4.元素。
http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
C#数组是零索引;也就是说,数组索引从零开始。