簡體   English   中英

Java類型不匹配-無法從構造函數中的int [] []錯誤轉換

[英]Java Type mismatch - cannot convert from int[][] error in constructor

我的構造函數是

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 [] []轉換為圖令牌上的語法錯誤:放錯位置的構造而是應使用變量聲明符

您必須像這樣創建矩陣:

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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM