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

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

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

问题

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

  1. public String toString() {
  2. int h;
  3. int j;
  4. String str;
  5. str = null;
  6. String[][] stringArray = new String[map.length][map.length];
  7. for (h = 0; h < getWidth(); h++) {
  8. for (j = 0; j < getHeight(); j++) {
  9. Cell i = getCell(h, j);
  10. stringArray[h][j] = i.toString() + " ";
  11. }
  12. }
  13. int k;
  14. int m;
  15. for (k = 0; k < stringArray.length; k++) {
  16. for (m = 0; m < stringArray.length; m++) {
  17. str = stringArray[k][m];
  18. }
  19. }
  20. return str;
  21. }

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

  1. public String toString() {
  2. if (status == ECellStatus.DEAD) {
  3. return ".";
  4. } else {
  5. return "#";
  6. }
  7. }

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

  1. . . . . .
  2. . . # . .
  3. . . . # .
  4. . # # # .
  5. . . . . .

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

英文:

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:

  1. public String toString() {
  2. int h;
  3. int j;
  4. String str;
  5. str = null;
  6. String[][] stringArray = new String[map.length][map.length];
  7. for (h = 0; h &lt; getWidth(); h++) {
  8. for (j = 0; j &lt; getHeight(); j++) {
  9. Cell i = getCell(h, j);
  10. stringArray[h][j] = i.toString() + &quot; &quot;;
  11. }
  12. }
  13. int k;
  14. int m;
  15. for (k = 0; k &lt; stringArray.length; k++) {
  16. for (m = 0; m &lt; stringArray.length; m++) {
  17. str = stringArray[k][m];
  18. }
  19. }
  20. return str;
  21. }

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

  1. public String toString() {
  2. if (status == ECellStatus.DEAD) {
  3. return &quot;.&quot;;
  4. } else {
  5. return &quot;#&quot;;
  6. }
  7. }

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

  1. . . . . .
  2. . . # . .
  3. . . . # .
  4. . # # # .
  5. . . . . .

I hope somebody can help me out.

答案1

得分: 2

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

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

  1. for (k = 0; k < stringArray.length; k++) {
  2. for (m = 0; m < stringArray.length; m++) {
  3. str += stringArray[k][m];
  4. }
  5. }

这还需要您将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:

  1. for (k = 0; k &lt; stringArray.length; k++) {
  2. for (m = 0; m &lt; stringArray.length; m++) {
  3. str += stringArray[k][m];
  4. }
  5. }

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:

确定