我正在尝试使用2D数组创建矩阵,但在矩阵中我想从数组中的另一个类中放置一个对象。我得到了一个"不兼容的类型"错误,我不明白为什么。矩阵应该如下所示: | (1,2,3)(1,2,3)| | (3,2,1)(3,2,1)|
这是我正在创建矩阵的类的构造函数。
public MatrixTriple2N(int n)
{
this.n=n;
int length=(int)Math.pow(2, n);
//Triple[][] matrix = new Triple [length][length];
MatrixTriple2N[][] matrix = new MatrixTriple2N [length][length]; //object array
for(int i=0; i<length; i++){
for(int j=0; j<length; j++){
matrix[i][j]=new Triple(); //having the problem here
}
}
}
这是我试图在MatrixTriple2N类中调用的类的代码和构造函数。
public class Triple {
private int a;
private int b;
private int c;
public Triple() {
a = b = c = 0;
}
public Triple(int p, int q, int r) {
a = p;
b = q;
c = r;
}
答案 0 :(得分:0)
我假设MatrixTriple2N是Triple
类型的对象矩阵。因此
MatrixTriple2N[][] matrix = new MatrixTriple2N[length][length];
可能不是你想要的。我假设你喜欢把它放在箱子里。像
Triple[][] matrix = new Triple[length][length];
现在您可以将新的Triple指定为该矩阵的元素。