c# - New Blank 2D array without size -


i need create array not know size in advance. used following, gives index out of bounds error in -loop.

string[] arr = s.split('\n'); int[] indices = new int[arr.length];  string[][] new_arr = new string[arr.length][];  (int = 0; < arr.length; i++) {     if (arr[i].startswith("21"))      {         indices[i] = i;     } }  indices = indices.where(val => val != 0).toarray(); //contains indices of records beginning 21  (int = 0; < indices.length - 1; i++) {      new_arr[0][i] = arr[i]; } //first n elements 

the error in second for-loop. says

object reference not set instance of object.

but did instantiate string @ beginning?

you need initialize each of arrays within jagged array:

indices = indices.where(val => val != 0).toarray(); //contains indices of records beginning 21  // create second-level array  new_arr[0] = new string[indices.length] ;  (int = 0; < indices.length - 1; i++) {     new_arr[0][i] = arr[i];  // why 0 here? }  

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -