英文:
How to create and return a enum in a method in java
问题
我有一个应该返回枚举的方法,但是我不太确定如何编写代码(这是我论文中的一个活动)。下面是这个方法(在名为 Maze 的类中):
private MazeStatus[][] stringToMaze(String sMaze) {
String[] splitString = sMaze.split("\n");
char[][] array = new char[splitString.length][];
MazeStatus[][] mazeStat;
for (int x = 0; x < splitString.length; x++) {
array[x] = new char[splitString[x].length()];
array[x] = splitString[x].toCharArray();
for (int y = 0; y < splitString[x].length(); y++) {
switch (array[x][y]) {
case '.':
mazeStat[x][y] = MazeStatus.VISITED;
break;
}
}
}
return null; // 待完成(第一部分):适当修改
}
这是 MazeStatus
类:
public enum MazeStatus {
OPEN(' '), OBSTACLE('#'), GOAL('x'), VISITED('.');
private char text;
MazeStatus(char s) {
this.text = s;
}
public char text() {
return this.text;
}
}
在方法中,我尝试创建一个枚举:
MazeStatus[][] mazeStat;
并向其中添加值:
mazeStat[x][y] = MazeStatus.VISITED;
但是当然它会抱怨没有初始化。我不知道如何初始化一个枚举,特别是一个二维数组的枚举,而不创建实际的 Maze 对象。
英文:
I have a method that should return a enum, however I am not to sure how to code this (it is a activity from my paper). Below is the method (in the class called Maze):
private MazeStatus[][] stringToMaze(String sMaze) {
String[] splitString = sMaze.split("\n");
char[][] array = new char[splitString.length][];
MazeStatus[][] mazeStat;
for (int x = 0; x < splitString.length; x++) {
array[x] = new char[splitString[x].length()];
array[x] = splitString[x].toCharArray();
for (int y = 0; y < splitString[x].length(); y++) {
switch (array[x][y]) {
case '.':
mazeStat[x][y] = MazeStatus.VISITED;
break;
}
}
}
return null; // TO DO (Part 1): change appropriately
}
Here is the MazeStatus
class:
public enum MazeStatus {
OPEN(' '), OBSTACLE('#'), GOAL('x'), VISITED('.');
private char text;
MazeStatus(char s) {
this.text = s;
}
public char text() {
return this.text;
}
}
In the method I have tried creating a enum
MazeStatus[][] mazeStat;
and adding values to it:
mazeStat[x][y] = MazeStatus.VISITED;
But of course it complains about it not being initialized. I do not know how to initialize a enum, especially a bidimensional array enum, without creating the actual Maze object.
答案1
得分: 4
你需要定义你的二维数组大小,就像这样:
MazeStatus[][] mazeStat = new MazeStatus[row.length][column.length];
我不知道在你的情况下确切的大小是多少,所以我使用了 row.length
和 column.length
。但请用在你的代码中有意义的值替换它们。
英文:
You need to define the size of your 2D array, like this:
MazeStatus[][] mazeStat = new MazeStatus[row.length][column.length];
I don't know exactly what the size will be in your case, that's why I've used row.length
and column.length
. But please replace these with values that make sense in your code.
答案2
得分: 1
private MazeStatus[][] stringToMaze(String sMaze) {
String[] splitString = sMaze.split("\n");
char[][] array = new char[splitString.length][];
// 这部分在你的代码中缺失。。。。
MazeStatus[][] mazeStat = new MazeStatus[splitString.length][column.length];
for (int x = 0; x < splitString.length; x++) {
array[x] = new char[splitString[x].length()];
array[x] = splitString[x].toCharArray();
for (int y = 0; y < splitString[x].length(); y++) {
switch (array[x][y]) {
case '.':
System.out.println("x" + x + " y" + y);
mazeStat[x][y] = MazeStatus.VISITED;
break;
default:
mazeStat[x][y] = MazeStatus.OPEN;
/*
* 这部分你必须相应地放置,因为如果没有默认条件,
* 那么它会取空值,然后抛出 NullPointerException
*/
}
}
}
return mazeStat; // 待完成(第一部分):适当修改
}
英文:
private MazeStatus[][] stringToMaze(String sMaze) {
String[] splitString = sMaze.split("\n");
char[][] array = new char[splitString.length][];
//this is missing in your code ........
MazeStatus[][] mazeStat = new MazeStatus[splitString.length][column.length];
for (int x = 0; x < splitString.length; x++) {
array[x] = new char[splitString[x].length()];
array[x] = splitString[x].toCharArray();
for (int y = 0; y < splitString[x].length(); y++) {
switch (array[x][y]) {
case '.':
System.out.println("x" + x + " y" + y);
mazeStat[x][y] = MazeStatus.VISITED;
break;
default:
mazeStat[x][y] = MazeStatus.OPEN;
/*
*this you have to put accordingly because if default
*condition won't be there then it will take null
*values and it will throw NullPointerException
*/
}
}
}
return mazeStat; // TO DO (Part 1): change appropriately
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论