Java Getters&;二传手及;混乱不堪
我的第二个类(MazeSolver)的问题是无法从第一个类(MazeInput)中获得行和列的值。这将导致在MazeSolver中我的drawMaze()方法中排列BoundsException和NullPointerException。我理解为什么会发生这些错误,我只是不知道如何解决它,因为我不知道如何将字段传递给其他类。你们这些可爱的家伙能指出我哪里出了问题吗Java Getters&;二传手及;混乱不堪,java,Java,我的第二个类(MazeSolver)的问题是无法从第一个类(MazeInput)中获得行和列的值。这将导致在MazeSolver中我的drawMaze()方法中排列BoundsException和NullPointerException。我理解为什么会发生这些错误,我只是不知道如何解决它,因为我不知道如何将字段传递给其他类。你们这些可爱的家伙能指出我哪里出了问题吗 public class LA2_MazeInput { private int rows; private int cols;
public class LA2_MazeInput {
private int rows;
private int cols;
public LA2_MazeInput() {
this.rows = getNumRows();
this.cols = getNumCols(rows);
}
private int getNumRows() {
int rows = 0;
String input;
input = JOptionPane.showInputDialog(
"Please enter the number of rows (5-10) for the maze.");
rows = Integer.parseInt(input);
return rows;
}
private int getNumCols(int numRows) {
int cols = 0;
String input;
input = JOptionPane.showInputDialog(
"Please enter the number (5-10) of columns for the maze."
+ "\n(Value cannot be the same as the number of rows.)");
cols = Integer.parseInt(input);
return cols;
}
public void initializeMazeSolver(/*MazeSolver solver*/) {
LA2_MazeSolver ms = new LA2_MazeSolver();
ms.setNumRows(this.rows);
ms.setNumCols(this.cols);
}
}
public class LA2_MazeSolver {
private int rows;
private int cols;
private String[][] maze;
public LA2_MazeSolver() {
this.maze = new String[rows][cols];
}
public void setNumRows(int numRows) {
this.rows = numRows;
}
public void setNumCols(int numCols) {
this.cols = numCols;
}
public int getNumRows() {
return this.rows;
}
public int getNumCols() {
return this.cols;
}
public void drawMaze() {
Random r = new Random();
maze[0][0] = "S";
maze[rows - 1][cols - 1] = "D";
int limit = ((rows * cols) / 3);
for (int i = r.nextInt(limit) + 1; i < limit; i++) {
maze[r.nextInt(rows - 1)][r.nextInt(cols - 1)] = "#";
}
for (int i = 0; i < maze.length; i++) {
for (int c = 0; c < maze[0].length; c++) {
if (!(maze[i][c].matches("#")) && !(maze[i][c].matches("S")) && !(maze[i][c].matches("D"))) {
maze[i][c] = Integer.toString(r.nextInt(100) + 1);
}
}
}
for (int i = 0; i < maze.length; i++) {
for (int c = 0; c < maze[0].length; c++) {
System.out.print(maze[i][c] + " ");
}
System.out.println();
}
}
}
公共类LA2_MazeInput{
私有int行;
私人公司;
公共LA2_MazeInput(){
this.rows=getNumRows();
this.cols=getNumCols(行);
}
私有int getNumRows(){
int行=0;
字符串输入;
input=JOptionPane.showInputDialog(
“请输入迷宫的行数(5-10)”;
行=整数.parseInt(输入);
返回行;
}
私有int-getNumCols(int-numRows){
int cols=0;
字符串输入;
input=JOptionPane.showInputDialog(
“请输入迷宫的列数(5-10)。”
+“\n(值不能与行数相同。)”;
cols=Integer.parseInt(输入);
返回cols;
}
公共void初始化MazeSolver(/*MazeSolver*/){
LA2_MazeSolver ms=新LA2_MazeSolver();
ms.setNumRows(this.rows);
setNumCols女士(this.cols);
}
}
公共级LA2_MazeSolver{
私有int行;
私人公司;
私有字符串[][]迷宫;
公共LA2_MazeSolver(){
this.maze=新字符串[行][cols];
}
公共void setNumRows(int numRows){
this.rows=numRows;
}
公共void setNumCols(int numCols){
this.cols=numCols;
}
public int getNumRows(){
返回该行;
}
public int getNumCols(){
归还这个.cols;
}
公共图书馆({
随机r=新随机();
迷宫[0][0]=“S”;
迷宫[rows-1][cols-1]=“D”;
整数限制=((行*列)/3);
对于(int i=r.nextInt(limit)+1;i
您的MazeSolver
构造函数初始化二维数组,大小为[0][0]
。之后,您尝试设置行和列,但此时已经太晚了
LA2_MazeSolver ms = new LA2_MazeSolver(); // constructor of LA2_MazeSolver is called
ms.setNumRows(this.rows); // does nothing for the array
ms.setNumCols(this.cols); // does nothing for the array
为了解决这个问题,您可以将参数与构造函数一起传递
public LA2_MazeSolver(int rows, int cols) {
this.maze = new String[rows][cols];
this.rows = rows; // in case you want them to store
this.cols = cols; // in case you want them to store
}
反模式:
public LA2_MazeSolver() {
}
public void setNumRows(int numRows) {
this.rows = numRows;
}
public void setNumCols(int numCols) {
this.cols = numCols;
}
public void init(){
this.maze = new String[rows][cols];
}
是这样的吗
LA2_MazeSolver ms = new LA2_MazeSolver();
ms.setNumRows(this.rows);
ms.setNumCols(this.cols);
ms.init();
我想了想,但在那一点上,我甚至需要getter和setter吗?@GregSmith,据我所知,你不需要它们。这是问题的一半……我想和构造函数一起做。然而,我需要有getter和setter,并按照教授的要求使用它们,我们遵循他的“基本程序设计”。有没有一种方法可以用getter和setter实现这一点?@GregSmith这是一种反模式。但是您可以使用第三种方法来初始化数组。最后,这一切都是关于在设置大小后不能更改固定数组的大小。否则,您将不得不创建一个新的。我编辑了我的答案,向大家展示了一个例子。我给我的教授发了一封电子邮件,询问我是否可以用不同于结构所说的方式来做这件事。我绝对同意这是一个反模式,但我们会看看这是否会改变什么。谢谢你的帮助,伙计,我很感激。