【发布时间】:2021-12-17 14:51:36
【问题描述】:
问题
我正在用 C# 编写一个 Matrix 类,我知道这个类需要一个二维数组来存储 Matrix 对象。我的 Matrix 类的构造函数有两个参数,即 Matrix 的 m 和 n 维。我试图在构造函数中分配数组,但我得到了错误
Invalid Rank Specifier。我试过的代码如下。
class Matrix
{
//make the dimensions private
private int m,n;
//The Matrix constructor needs two parameters
//The row and column dimensions
//declare the two dimension array
private int[][] array;
public Matrix(int m, int n) {
//assign the array inside the constructor
array = new int[m][n];
}
//method to allocate the Matrix
}
我的目标是先用维度初始化数组,以便可以在没有错误的情况下完成分配,谢谢。
【问题讨论】:
-
第一种选择是使用多维数组而不是锯齿状数组,第二种是使用循环分配每个叶子数组
-
@ChrᴉzremembersMonica,但我只是使用需要存储在该对象中的数组后面的 new 关键字对其进行了初始化
-
@ChrᴉzremembersMonica,打开链接,同样的问题,谢谢。