这是一段代码作为示例,剩下的只是方法(在底部查看迷宫类)。所以当它被实例化时,使用
Maze labyrinth = new Maze();
和
System.out.println (labyrinth);
这将打印出网格数组。这是合法的吗?我认为所有类都需要构造函数它如何打印出二维网格数组?
迷宫类:
public class Maze
{
private final int TRIED = 3;
private final int PATH = 7;
private int[][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,0,1,0,1,0,0},
{1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1} };
public String toString ()
{
String result = "\n";
for (int row = 0; row < grid.length; row++)
{
for (int column=0; column < grid[row].length; column++)
result += grid[row][column] + "";
result += "\n";
}
return result;
}
}