我想为我的数组添加一个新维度。基于构造函数中用户输入的Matrix类。
示例:
var matrix = new Matrix<int>(3,2,4); // is 3 dimensional array
这是我的Matrix
班:
class Matrix<T>
{
private T[][] mArray;
private readonly int mCols;
private readonly int mRows;
public Matrix(params int[] args)
{
//here is what I ve tried.
/*
mCols = args[0];
mRows = args[1];
mArray = new T[mCols][];
for (int i = 0; i < mCols; i++)
mArray[i] = new T[mRows];
*/
// how to create a multidimensional array based on "args" length?
}
}
问题:如何基于“ args”长度创建多维数组?
答案 0 :(得分:3)
通过查看here
找到了它 Array.CreateInstance(typeof(YOUR_TYPE), params)
示例:
var arr = Array.CreateInstance(typeof(int), 3, 2, 4); // creates a 3 dimensional array