2D matrix java bluej



>我正在尝试使用 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;
   }

我假设MatrixTriple2N是Triple类型的对象的矩阵。因此

MatrixTriple2N[][] matrix = new MatrixTriple2N[length][length];

可能不是您想要的。我假设你喜欢装箱 s.th。喜欢

Triple[][] matrix = new Triple[length][length];

现在,您可以将新的三元组分配给该矩阵的元素。

最新更新