如何在java中初始化没有值的多维数组([])?
所以我在写一个迷宫解算器,在我的迷宫课程中,我在迷宫的2d网格中使用一个自定义“正方形”对象的数组如何在java中初始化没有值的多维数组([])?,java,multidimensional-array,Java,Multidimensional Array,所以我在写一个迷宫解算器,在我的迷宫课程中,我在迷宫的2d网格中使用一个自定义“正方形”对象的数组 public class Maze { int height; int width; Square[][] grid; //2d array of squares representing the maze Square startSquare; Square exitSquare; ... public static Square[][] gridMaker(int h, int w, L
public class Maze {
int height;
int width;
Square[][] grid; //2d array of squares representing the maze
Square startSquare;
Square exitSquare;
...
public static Square[][] gridMaker(int h, int w, List<Integer> squares){
int count=0;
Square[][] newgrid;
for (int i=0; i<h; i++){
for (int j=0; j<w; j++){
Square element = new Square(squares.get(count),i,j);
newgrid[i][j] = element;
count++;
}
}
return newgrid;
}
公共类迷宫{
内部高度;
整数宽度;
正方形[][]网格;//表示迷宫的二维正方形数组
四方形;
广场出口广场;
...
公共静态方块[][]gridMaker(整数h、整数w、列表方块){
整数计数=0;
正方形[][]新网格;
对于(inti=0;i来说,您缺少了一些非常明显的东西
Square[][] newgrid = new Square[h][w];
根据您在此处提交的内容,在gridMaker方法中声明2D数组newgrid时,您只需要在此处实例化它。因此,根据您的其余代码,它应该是:
Square[][] newgrid = new Square[h][w];
实例化后,您应该能够为数组中的索引赋值,从而消除错误。Square[][]newgrid=newint[h][w];