.net - I need to create 2D array in C# -


i need create 2d jagged array. think of matrix. number of rows known, number of columns not known. example need create array of 10 elements, type of each element string[]. why need that? number of columns not known - function must allocation , pass array other function.

string[][] creatematrix(int numrows) {  // function must create string[][] numrows first dimension. } 

update

i have c++ background. in c++ write following (nevermind syntax)

double ** createarray() {  double **parray = new *double[10]() // create 10 rows first } 

update 2

i considering using list, need have indexed-access both rows , columns.

you can write:

string[][] matrix = new string[numrows][]; 

and produce 2d array of null elements. if want fill array non-null items, need write:

for (int row = 0; row < numrows; row++) {     matrix[row] = new string[/* col count row */]; } 

you need specify column count each row of matrix. if don't know in advance, can either.

  1. leave matrix items unintialised, , populate them know size.
  2. use fixed number give room enough maximum possible size.
  3. avoid arrays , use list<> suggested others here.

hope helps.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -