2015-04-25 35 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");