I need to create an array for which I do not know the size in advance. I used the following, but it gives an index out of bounds error in my for -loop.
string[] arr = s.Split('\n');
int[] indices = new int[arr.Length];
string[][] new_arr = new string[arr.Length][];
for (int i = 0; i < arr.Length; i++)
{
if (arr[i].StartsWith("21"))
{
indices[i] = i;
}
}
indices = indices.Where(val => val != 0).ToArray(); //contains indices of records beginning with 21
for (int i = 0; i < indices.Length - 1; i++)
{
new_arr[0][i] = arr[i];
} //first n elements
The error is in the second for-loop. It says
Object reference not set to an instance of an object.
But I did instantiate the string at the beginning?