gpt4 book ai didi

java - 在子类上创建矩阵

转载 作者:行者123 更新时间:2023-12-02 04:40:29 25 4
gpt4 key购买 nike

我创建了一个 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}};
}
}

最佳答案

  • 我们没有 Settore 类。
  • Matrice 是私有(private)的,因此无法在 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}
});

关于java - 在子类上创建矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30241351/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com