2013-10-21 79 views
0

我試圖創建一個使用二維陣列的矩陣,但矩陣我希望把一個對象從另一個類在數組中。我得到一個「不兼容類型」的錯誤,我不明白爲什麼。矩陣應如下所示: | (1,2,3)(1,2,3)| | (3,2,1)(3,2,1)|二維矩陣的Java BlueJ的

這是我創建的矩陣類的構造函數。

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

我假設MatrixTriple2N是類型爲Triple的對象的矩陣。因此

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

可能你正打算不算什麼。我假設你喜歡s.th.像

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

現在您可以指定一個新的三元組作爲該矩陣的元素。