2015-04-25 37 views
0

當我嘗試將參數傳遞給構造函數時,爲什麼會出現紅色下劃線錯誤,即創建對象?我究竟做錯了什麼?傳遞給構造函數時出錯

public static void main(String[] args) 
    { 
     CreateShape temp = new CreateShape(3,3, 'a', 
       {{'x','.','.'} 
       {'.','.','x'} 
       {'x','.','x'}}, "x . .\n" 
          + ". . x\n" 
          + "x . x"); 
     temp.rotateCW(); 
     System.out.println(temp); 
public CreateShape(int height, int width, char dc, char[][] charLayout, String layout) 
    { 
     this.height = height; 
     this.width = width; 
     this.dc = dc; 
     this.shape = charLayout; 
     this.layout = layout; 
     initialPos = Rotation.CW0; 
    } 

通過編寫參數爲char[][],我做了一些錯誤。

+3

缺少逗號行的二維數組 – Alejandro

+3

在分開......還缺少'新的char [] []'部分:'新的char [] [] {{。 ..},{...},{...}}' –

回答

1

我假設rotateCW方法和字段都是在類中聲明的。

當定義一個二維數組時,該數組被讀取爲一個數組的數組。在一維數組中,我們使用{entry,entry}。同樣在2D數組中,{{entry,entry},{entry,entry}}。另外,一個數組是一個對象,必須像這樣構造。

你的問題是,你沒有數組構造函數,也有不陣列之間用逗號......所以陣列應該被定義爲:

new char[][]{ 
    {'x','.','.'}, 
    {'.','.','x'}, 
    {'x','.','x'}} 

,然後的剩餘參數照常。

0

char數組必須在2d中用逗號分隔。這會爲你工作,我猜

CreateShape temp = new CreateShape(3,3, 'a', 
       new char[][]{{'x','.','.'}, 
       {'.','.','x'}, 
       {'x','.','x'}}, "x . .\n" 
          + ". . x\n" 
          + "x . x");