Java类型不匹配 - 无法从构造函数中的int [] []错误转换

Yar*_*arh 0 java

我的构造函数是

public class Figure{
    int[][] x;
    Color y;
    public Figure(int[][] x , Color y){
        this.x=x;
        this.y=y;
    }
Run Code Online (Sandbox Code Playgroud)

我正在通过以下方式初始化对象:

Figure s = new Figure({{0,1,1},{1,1,0}},Color.ORANGE);
Run Code Online (Sandbox Code Playgroud)

收到以下错误:

类型不匹配 - 无法从int [] []转换为图令牌上的语法错误:错误的构造变量声明符预期而不是

Mar*_*aux 9

你必须像这样创建矩阵:

new Figure(new int[][]{{0,1,1}, {1,1,0}},Color.ORANGE);
Run Code Online (Sandbox Code Playgroud)

或者不那么脏的方法:将矩阵结构分布在几行上:

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

new Figure(matrix, Color.ORANGE);
Run Code Online (Sandbox Code Playgroud)

  • 这应该工作:`new Figure(new int [] [] {{0,1,1},{1,1,0}},Color.ORANGE);` (2认同)