2로 인덱싱 된 행뿐만 아니라 모든 행에 대해이 루프를 반복해야합니다. 또한 이중 구문 분석에서 오류가 발생합니다.C#을 사용하여 알 수없는 행에 대한 루핑
다음을 수행하는 효율적인 방법이 있습니까?
파일의 행 길이는 가변적 일 수 있습니다. 따라서 배열을 유지하여 각 행의 크기를 저장해야합니다.
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;
}
}