我需要为所有行循环此循环,而不仅仅是由 2 索引的行。此外,我在双重解析中遇到错误。
有没有一种有效的方法来执行以下操作?
该文件可以具有可变长度的行。因此我需要维护一个数组来存储每一行的大小。
public static void ReadFile()
{
int lineNo;
List<List<double>> numbers = new List<List<double>>();
foreach (string line in File.ReadAllLines("Data.txt"))
{
var list = new List<float>();
foreach (string s in line.Split(new[]{',', ' '}, StringSplitOptions.RemoveEmptyEntries))
{
int i;
if(double.TryParse(s, out i))
{
list.Add(i);
lineNo++;
}
}
numbers.Add(list);
}
var specialNumber = numbers[3][4]; // gives line 3 number 4
var specialLine = numbers[2].ToArray(); // gives an array of numbers of line 2
double[] rowTotal;
double[] squareRowTotal;
double[] rowMean;
//I need to loop this loop for all rows and not just the row indexed by 2. Also I am getting an error in the double parsing.
for (int j=0; j<(specialLine.Length); j++)
{
rowTotal[2] = rowTotal[2] + numbers[2][j];
squareRowTotal[2] = squareRowTotal[2] + numbers[2][j] * numbers[2][j];
}
for (int k = 0; k < lineNo; k++)
{
rowMean[k] = rowTotal[k] / numbers[k].Length;
}
}