【发布时间】:2018-09-17 20:02:17
【问题描述】:
找到可行的解决方案后,我试图了解为什么我的解决方案不起作用。我添加了最小工作示例(MWE),首先我展示了工作解决方案,然后我在 1 和 2 展示了一个尝试的解决方案。我手动打印数组的所有元素,然后再次在 for 循环中打印。
由此我得出结论,首先评估外部 for 循环(带有计数器 i),然后评估内部 for 循环(带有计数器 q)。我很困惑,想知道我对 Java for 循环的评估顺序的理解是否不正确,或者我是否有我遗漏的语法/不同错误。
问题:谁能提出一个 for 循环评估顺序的原因?
public class LearnMultiDimensionalArrayLength {
public static void main(String[] args) {
// Generate array
double[][] a = {{1.5, -10.3, 0}, {-2.5, 8, 1.3}};
// call method that loops through the array
forloops(a);
}
public static void forloops(double[][] a) {
// Working solution:
//loops through all elements of the array and prints their values.
for (double[] x : a)
{
for (double y : x)
{
System.out.print(y + " ");
}
System.out.println();
}
// Now I tried my own solution:
System.out.println("The first dimension length: "+ a.length +
" The second dim. length: " + a[1].length);
System.out.println("first dim-1 ="+ (a.length -1));
System.out.println("Second dim-1 ="+ (a[1].length -1));
// Question to self: Why is the following for loop wrong?
// for (int i=0;i <= a.length;i++){
// for (int q=0;q <= a[1].length;i++){
// System.out.println("["+i+"]"+"["+q+"]"+"="+a[i][q]);
// }
// }
// 1. Attempted Answer: Because the lengths are the nr of rows
// and nr of columns in the matrix. so with n = 2, there are only the elements
// 0 and 1 in the first []
// Test hypotheses by looping through the array a with length-1:
// for (int i=0;i <= a.length-1;i++){
// for (int q=0;q <= a[1].length-1;i++){
// System.out.println("["+i+"]"+"["+q+"]"+"="+a[i][q]);
// }
// }
// Conclusion, that did solve the problem.
// 2. Attempt: Print all elements and their values, then compare
// with what the for loop does:
System.out.println("a[0][0]="+a[0][0]);
System.out.println("a[0][1]="+a[0][1]);
System.out.println("a[0][2]="+a[0][2]);
System.out.println("a[1][0]="+a[1][0]);
System.out.println("a[1][1]="+a[1][1]);
System.out.println("a[1][2]="+a[1][2]);
// System.out.println("a[2][0]="+a[2][0]);
// System.out.println("a[2][1]="+a[2][1]);
// System.out.println("a[2][2]="+a[2][2]);
// 2.a Troubleshooting: verify the upper bound of the for loops
// are not producing an error due to differences in types:
System.out.println(((Object)1).getClass().getName());
System.out.println(((Object)a[1].length).getClass().getName());
System.out.println(((Object)(a[1].length-1)).getClass().getName());
// All expressions are evaluated as integer, both i and q are integers
// hence the different types should not result in a false negative.
// The actual problematic for loop:
System.out.println("Boolean evaluation:" + (1 == (a.length-1)));
for (int i=0; (i <= (a.length-1));i++){
for (int q=0; (q <= (a[1].length-1));i++){
//System.out.println("retry" + "[i="+i+"]"+"[q="+q+"]"+"="+a[i][q]);
System.out.println("This is the wrong order of for loops" + "[q="+q+"]"+"[i="+i+"]"+"="+a[q][i]);
}
}
}
}
【问题讨论】:
-
只需编写一个带有嵌套循环的简单程序,并在每个循环中添加一个 println 语句,您就会看到它们是如何执行的。
-
在您的内部循环中,您遇到了复制粘贴错误:您仍在递增
i,但您的循环计数器为q。 -
另外,您似乎已经翻转了列和行。在第一个解决方案(Foreach)中,对于所有行,您打印所有列。但是在有问题的循环中,你有 i (这是第一个,所以那是行对吗?)然后是 q (所以列)但是在打印中你需要 a[q][i] (它应该是 a[row][column] 到匹配 foreach [也匹配循环条件而不获取 IndexOutOfBounds])
-
@azurefrog 谢谢,这确实是问题所在,而且它似乎是以相反的顺序评估 for 循环的原因。
-
@Worthless 确实感谢您的反馈,在我的回答中,我再次将它们换回来。
标签: java multidimensional-array nested order-of-execution