Jagged arrays should be initialized in two steps:
The array itself:
int[][] Scores = new int[3][];
The sub-arrays:
Scores[0] = new int[12]; Scores[1] = new int[8]; Scores[2] = new int[10];
Array is a fixed length data structure. If you don't know the size in advance, you have to use a dynamic length structure. The best option is List<>
class:
List<List<int>> scores = new List<List<int>>();
scores.Add( new List<int>() );
using( StreamReader section1read = File.OpenText("Section1.txt"))
{
string line;
while ((line = section1read.ReadLine()) != null)
{
scores[0].Add(int.Parse(line));
}
}
Here are other things to consider:
- Use a
using
block make sure any unmanaged resources associated with file is disposes. - You can check the return value of
StreamReader.ReadLine()
to determine the end of file