Cap*_*mic 2 .net c# arrays multidimensional-array
我需要创建2D锯齿状数组.想想一个矩阵.行数是已知的,列数是未知的.例如,我需要创建10个元素的数组,其中每个元素的类型为string [].我为什么需要那个?列数是未知的 - 此函数必须简单地执行分配并将数组传递给其他函数.
string[][] CreateMatrix(int numRows)
{
// this function must create string[][] where numRows is the first dimension.
}
Run Code Online (Sandbox Code Playgroud)
UPDATE
我有C++背景.在C++中,我会编写以下内容(从不修改语法)
double ** CreateArray()
{
double **pArray = new *double[10]() // create 10 rows first
}
Run Code Online (Sandbox Code Playgroud)
更新2
我正在考虑使用List,但我需要对行和列进行索引访问.
无法做到.但是你可以这样做:
List<List<string>> createMatrix(int numRows)
{
return new List<List<string>>(numRows);
}
Run Code Online (Sandbox Code Playgroud)
这使您能够在第二个维度中拥有灵活数量的对象.