我创建了一个Mappa类,其方法是创建一个数值矩阵Matrice,然后我创建了一个子类MappaFermi,并且我已经编写了矩阵的特定内容但是有一个错误说数组常量只能用作初始化者,有什么建议吗?
public class Mappa {
private final Settore [][] settore;
private int Matrice [][];
private static final int X=23;
private static final int Y=14;
public Mappa (){
settore = new Settore[X][Y];
for (int i=0; i < X; i++){
for (int j=0; j<Y; j++) {
settore[i][j] = new Settore (i,j);
}
}
Matrice = new int[23][14];
}
public int[][] getMatrice() {
return Matrice;
}
public void setMatrice(int matrice[][]) {
Matrice = matrice;
}
}
public class MappaFermi extends Mappa {
public MappaFermi() {
Matrice={{0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,2,1,2,1,2,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,3,0,1,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,0,0,0,1,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
}
}
答案 0 :(得分:2)
Settore
。Matrice
是私有的,因此无法在MappaFermi
中访问,您需要使用setMatrice
方法。new int[][]
setMatrice(new int[][]
{
{0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,2,1,2,1,2,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,3,0,1,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0},
{0 ,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{0 ,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0 ,0,0,0,0,0,0,1,1,1,0,0,0,1,2,1,0,0,0,0,0,0,0},
{0 ,0,0,0,0,0,0,1,0,1,1,1,1,1,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0 ,0,0,0,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
});
答案 1 :(得分:0)
您使用的语法无效。内联数组初始化具有以下形式:
Matrice = new int[][] {{//your definition
}}
答案 2 :(得分:0)
如果要像这样初始化它,则需要在方法之外初始化它。
public class MappaFermi extends Mappa {
Matrice={{0,0,0,0,0,0,0,0,0,3,0,0,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,1,0,2,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,2,1,2,1,2,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,2,2,2,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,3,0,1,0,3,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,1,0,0,1,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,1,1,0,0,0,1,2,1,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,1,0,1,1,1,1,1,0,2,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,2,1,0,1,0,2,1,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,2,0,1,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}};
}
public MappaFermi() {
}
}
答案 3 :(得分:0)
如果您正在使用数组初始化程序={{}}
,则无法在基类中以及以任何其他方式在基类和赋值private int Matrice [][];
中拆分声明Matrice = {{}}
。
如果要使用表单={{}};
请通过遍历每个元素(例如
)来设置Matrice
上的值
Matrice[i][j] = <value>;
如果你在基类中使用它。