正在从文本文件中读取二维数组
本文关键字:读取 二维数组 文件 文本 | 更新日期: 2023-09-27 17:57:36
我所做的是从一个文本文件中读取内容,然后将内容存储在一个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]
。。。它应该是CCD_ 3。下面是我的代码。我已经花了很多时间在这上面了,有人能帮忙吗?
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开始。所以索引3就是4。要素
http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
C#数组是零索引的;也就是说,数组索引从zero开始。