I need to create 2D jagged array. Think of a matrix. The number of rows is known, the number of columns is not known. For example I need to create array of 10 elements, where type of each element string[]. Why do I need that? The number of columns is not known – this function must simply do the allocation and pass array to some other function.
string[][] CreateMatrix(int numRows)
{
// this function must create string[][] where numRows is the first dimension.
}
UPDATE
I have C++ background. In C++ I would write the following (nevermind the syntax)
double ** CreateArray()
{
double **pArray = new *double[10]() // create 10 rows first
}
UPDATE 2
I was considering using List, but I need to have indexed-access to both rows and columns.
You can write:
and that will produce a 2D array of
nullelements. If you want to fill the array with non-null items, you need to write:You need to specify the column count for each row of the matrix. If you don’t know it in advance, you can either.
List<>as suggested by others here.Hope this helps.