如何在Java方法中创建并返回枚举。

huangapple go评论97阅读模式
英文:

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(&quot;\n&quot;);
    char[][] array = new char[splitString.length][];
    MazeStatus[][] mazeStat;

    for (int x = 0; x &lt; splitString.length; x++) {
        array[x] = new char[splitString[x].length()];
        array[x] = splitString[x].toCharArray();
        for (int y = 0; y &lt; splitString[x].length(); y++) {
            switch (array[x][y]) {
                case &#39;.&#39;:
                    mazeStat[x][y] = MazeStatus.VISITED;
                    break;
            }
        }
    }
    return null; // TO DO (Part 1): change appropriately
}

Here is the MazeStatus class:

public enum MazeStatus {
    OPEN(&#39; &#39;), OBSTACLE(&#39;#&#39;), GOAL(&#39;x&#39;), VISITED(&#39;.&#39;);

    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.lengthcolumn.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(&quot;\n&quot;);
    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 &lt; splitString.length; x++) {
        array[x] = new char[splitString[x].length()];
        array[x] = splitString[x].toCharArray();
        for (int y = 0; y &lt; splitString[x].length(); y++) {
            switch (array[x][y]) {
                case &#39;.&#39;:
                    System.out.println(&quot;x&quot; + x + &quot; y&quot; + y);
                    mazeStat[x][y] = MazeStatus.VISITED;
                    break;
                default:
                    mazeStat[x][y] = MazeStatus.OPEN;
                    /*
                     *this you have to put accordingly because if default 
                     *condition won&#39;t be there then it will take null
                     *values and it will throw NullPointerException
                     */
            }
        }
    }
    return mazeStat; // TO DO (Part 1): change appropriately
}

huangapple
  • 本文由 发表于 2020年4月6日 15:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/61054987.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定