2012-07-25 101 views
0

我的構造函數是的Java類型不匹配 - 不能從int [] []錯誤轉換在構造

public class Figure{ 
    int[][] x; 
    Color y; 
    public Figure(int[][] x , Color y){ 
     this.x=x; 
     this.y=y; 
    } 

,我在下面的方式初始化對象:

Figure s = new Figure({{0,1,1},{1,1,0}},Color.ORANGE); 

得到以下錯誤:

類型不匹配 - 無法從int [] []轉換爲圖 令牌上的語法錯誤:錯位的結構 變量聲明符expec泰德反而

回答

9

你必須創建這樣的矩陣:

new Figure(new int[][]{{0,1,1}, {1,1,0}},Color.ORANGE); 

還是不太骯髒的方式:傳播矩陣構造了幾行:

int[][] matrix = new int[2][]; 
matrix[0] = new int[]{0,1,1}; 
matrix[1] = new int[]{1,1,0}; 

new Figure(matrix, Color.ORANGE); 
+2

這應該工作:'新圖(new int [] [] {{0,1,1},{1,1,0}},Color.ORANGE);' – Autar 2012-07-25 12:46:19

+0

感謝它的工作原理:) 我應該叫新對象[] evry時間我是使用數組或列表作爲參數? – Yarh 2012-07-25 12:49:07

+0

@Autar:謝謝,它確實有效。 – 2012-07-25 12:49:51

相關問題