我一直在工作一个大小适中但单线程的Java应用程序。在其中,我有一个循环遍历大约size [300] [10]的矩阵的函数。在此代码段上方,我已经做了很多事情,还有其他3个与局部变量大小相似的矩阵。当我发现以下代码时,我遇到了循环无法遍历第一个值(table [0] [0])的问题:
System.out.println("");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if(i == 0 && j == 0){System.out.println("looped through 0 0");}
// a bunch of other stuff
}
}
打印出:
looped through 0 0
但代码:
//System.out.println("");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if(i == 0 && j == 0){System.out.println("looped through 0 0");}
// a bunch of other stuff
}
}
不打印任何内容。
为什么会这样?我是否用完了Java堆空间?我溢出了吗?这是编译器错误吗?
答案 0 :(得分:1)
我发现了问题所在。在循环上方,我有另一个循环,并在其中留下了printf语句。我仍然不确定为什么会发生这种情况,但是我可以在下一节中重现它。
public class TestJavaPrintfError {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
System.out.printf(" ");
}
String[][] table = new String[300][6];
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
if (i == 0 && j == 0) {
System.out.println("looped through 0 0");
}
}
}
}
}
我正在开发日食霓虹灯。有人知道为什么会这样吗?
答案 1 :(得分:0)
这是个玩笑吗?
只需将控制台滚动到最右边,即可看到打印内容。
您使用System.out.printf(" ");
100次,它会输出很多空格并将您的笔尖向右推。