【发布时间】:2015-01-17 23:15:27
【问题描述】:
所以我正在尝试创建一个 toString 方法,以便能够调试我的代码以检查方法是否在扫描文本文件后正确构建网格,它为每个对象编号并将其放置在 6x6 网格上。它打印出如下内容:
0000100
1196108
0006108
224608
355508
300777
但是,当我使用 println 检查网格时,位置 1,5 和 2,5 的整数是 8,而不是零...为什么要添加 3 个额外字符?
public String toString()
{
// StringBuilder used for storing the grid as a string instead of an int
// array
StringBuilder display = new StringBuilder(36);
for (int row = 0; row < 6; row++)
{
// Moves cursor to next line
display.append("\n");
// Appends each integer of the row one by one
for (int col = 0; col < 6; col++)
{
display.append(boardGrid[row][col]);
}
}
return display.toString();
}
似乎无法发现问题所在。
【问题讨论】:
-
boardGrid是什么类型,你是如何填充它的? -
在这里进行猜测,但所有“长”的都包含模式 10。你知道,就像数字 10。也许附加类似 boardGrid[row][col] 的内容,然后是逗号,看看你会得到什么。
-
它是一个二维整数数组,它通过同一类中的另一个方法填充,该方法采用“块”对象的 ArrayList 来确定块数,然后扫描文本文件以获取块的数据(如果它可以垂直移动,它的大小,它的 x,y 位置,它的长度)如果你愿意,我可以发布整个方法
-
天哪,不敢相信我没看到,谢谢@James!
-
@ShadowBlade 很高兴我能帮上忙。