How to make a toString() method for a 2D Array of Cells using the toString() method of Cell?

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

How to make a toString() method for a 2D Array of Cells using the toString() method of Cell?

问题

我的任务是实现康威的生命游戏,但我卡在了如何在 toString() 方法中表示我的游戏地图(一个二维单元格数组)上。
我的toString()方法:

public String toString() {
    int h;
    int j;
    String str;
    str = null;
    String[][] stringArray = new String[map.length][map.length];
    for (h = 0; h < getWidth(); h++) {
        for (j = 0; j < getHeight(); j++) {
            Cell i = getCell(h, j);
            stringArray[h][j] = i.toString() + " ";
        }
    }
    int k;
    int m;
    for (k = 0; k < stringArray.length; k++) {
        for (m = 0; m < stringArray.length; m++) {
            str = stringArray[k][m];
        }
    }
    return str;
}

正如你所看到的,我需要调用特定于单元格的 toString() 方法作为任务的一部分。
这个 toString() 方法如下:

public String toString() {
    if (status == ECellStatus.DEAD) {
        return ".";
    } else {
        return "#";
    }
}

在实际代码中,我只获得了最后一个单元格的表示,但我想要像这样打印它:

. . . . .  
. . # . .  
. . . # .  
. # # # .  
. . . . .  

希望有人可以帮助我解决这个问题。

英文:

My task is to implement Conway's Game of Life, but I'm stuck in representing my game-map (a 2D Array of Cells) in the toString() method.
My toString() method:

public String toString() {
    int h;
    int j;
    String str;
    str = null;
    String[][] stringArray = new String[map.length][map.length];
    for (h = 0; h &lt; getWidth(); h++) {
        for (j = 0; j &lt; getHeight(); j++) {
            Cell i = getCell(h, j);
            stringArray[h][j] = i.toString() + &quot; &quot;;
        }
    }
    int k;
    int m;
    for (k = 0; k &lt; stringArray.length; k++) {
        for (m = 0; m &lt; stringArray.length; m++) {
            str = stringArray[k][m];
        }
    }
    return str;
}

And as you see I need to call the cell specific toString() method as part of the task.
This toString() method looks like this:

public String toString() {
    if (status == ECellStatus.DEAD) {
        return &quot;.&quot;;
    } else {
        return &quot;#&quot;;
    }
}

In the actual code I only get the representation of the last cell but I want to print it like that:

. . . . .  
. . # . .  
. . . # .  
. # # # .  
. . . . .  

I hope somebody can help me out.

答案1

得分: 2

在第二个for循环中,您正在将str重新分配给当前单元格的toString,而不是在其上附加/添加,这可能是您打算做的操作。

要附加到str,您可以执行以下操作:

for (k = 0; k < stringArray.length; k++) {
    for (m = 0; m < stringArray.length; m++) {
        str += stringArray[k][m];
    }
}

这还需要您将str初始化为非空值,所以代替
str = null; 您需要执行 str = ""; 或类似的操作。

英文:

In the second for-loop, you are reassigning str to the current Cell's toString rather than appending/adding on to it, which is probably what you intended to do.

To append to str, you can do:

for (k = 0; k &lt; stringArray.length; k++) {
    for (m = 0; m &lt; stringArray.length; m++) {
        str += stringArray[k][m];
    }
}

This will also require you to initialize str to a non-null value, so instead of
str = null; you will need to do str = &quot;&quot;; or some equivalent of it.

huangapple
  • 本文由 发表于 2020年7月28日 01:05:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63120097.html
匿名

发表评论

匿名网友

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

确定