java 简单的嵌套 for 循环示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/656747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple nested for loop example
提问by ???
Currently I am studying for my Java test. Whist studying I've come across a small problem.
目前我正在学习我的 Java 测试。在学习期间,我遇到了一个小问题。
In this for loop:
在这个 for 循环中:
for ( int i=1; i <= 3 ; i++ ) {
for (int j=1; j <= 3 ; j++ ) {
System.out.println( i + " " + j );
}
}
The output is:
输出是:
1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
My problem is, I don't understand it. When I read this code I keep thinking it should look like this:
我的问题是,我不明白。当我阅读这段代码时,我一直认为它应该是这样的:
1 1
2 2
3 3
Why is this not the case?
为什么不是这样?
回答by Reed Copsey
Each iteration of i, you're starting a completely new iteration of j.
i 的每次迭代,您都在开始一个全新的 j 迭代。
So, you start with i==1, then j==1,2,3 in a loop. Then i==2, then j==1,2,3 in a loop, etc.
所以,你从 i==1 开始,然后循环中的 j==1,2,3。然后 i==2,然后循环中的 j==1,2,3,等等。
Step through it one step at a time, and it will make sense.
一步一步地完成它,这将是有意义的。
回答by Smashery
What you have is one loop inside of another. Think of it like the minute hand and the hour hand on a clock. The minute hand is going to go around (1,2,3,4...59) while the hour hand is still on 1. So, if we treat hours as i, and minutes as j, we have:
你所拥有的是一个循环在另一个循环中。把它想象成时钟上的分针和时针。分针将走动 (1,2,3,4...59) 而时针仍在 1 上。因此,如果我们将小时视为 i,将分钟视为 j,我们有:
1 1
1 2
1 3
...
1 60
1 1
1 2
1 3
...
1 60
And then the hours change to 2, and the minute hand keeps going around:
2 1
2 2
2 3
...
2 60
然后小时变为 2,分针继续走动:
2 1
2 2
2 3
...
2 60
And we finish once we get to
一旦我们到达,我们就完成了
12 1
12 2
12 3
...
12 60
12 1
12 2
12 3
...
12 60
which is where the loop ends. Your example is much like this.
这是循环结束的地方。你的例子很像这样。
(For the pedantic, I know it's zero-based, but in order to illustrate, this may be easier to understand)
(对于学究,我知道它是零基础的,但为了说明,这可能更容易理解)
回答by Michael Myers
Let's look at what this would look like if we changed from loops to repeated code. First the outer loop:
让我们看看如果我们从循环改为重复代码会是什么样子。首先是外循环:
for (int j=1; j <= 3 ; j++ ) {
System.out.println( 1 + " " + j );
}
for (int j=1; j <= 3 ; j++ ) {
System.out.println( 2 + " " + j );
}
for (int j=1; j <= 3 ; j++ ) {
System.out.println( 3 + " " + j );
}
Now the inner loop:
现在内部循环:
// First loop
System.out.println( 1 + " " + 1 );
System.out.println( 1 + " " + 2 );
System.out.println( 1 + " " + 3 );
// Second loop
System.out.println( 2 + " " + 1 );
System.out.println( 2 + " " + 2 );
System.out.println( 2 + " " + 3 );
// Third loop
System.out.println( 3 + " " + 1 );
System.out.println( 3 + " " + 2 );
System.out.println( 3 + " " + 3 );
Does it make sense now?
现在有意义吗?
回答by Upul Bandara
For each iteration of outer loop, complete inner loop will execute. For example when i= 1, inner loop will execute 3 times and you will get 1 1 1 2 1 3
对于外循环的每次迭代,将执行完整的内循环。例如当 i=1 时,内循环将执行 3 次,您将得到 1 1 1 2 1 3
回答by Retsam
Every time you nest for loops (that's what it's called when you put one inside of another), it basically adds another "dimension". If you have a single for loop, it's like a straight line. So, if our first loop is from 1 to 5 it would be:
1 2 3 4 5
每次嵌套 for 循环时(这就是将一个循环放入另一个循环时的名称),它基本上会添加另一个“维度”。如果你有一个 for 循环,它就像一条直线。因此,如果我们的第一个循环是从 1 到 5,它将是:
1 2 3 4 5
If you add a second loop, (lets say 1-3) (You read top to bottom left to right, first number represents the first variable, etc)
如果您添加第二个循环,(假设为 1-3)(您从左到右阅读从上到下,第一个数字代表第一个变量等)
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
11 21 31 41 51
12 22 32 42 52
13 23 33 43 53
And if you add a third loop (let's say 1-2) (I obviously can't make 3 dimensions here, so bear with me)
如果您添加第三个循环(假设为 1-2)(我显然无法在这里制作 3 个维度,所以请耐心等待)
111 211 311 411 511 || 112 212 312 412 512
121 221 321 421 521 || 122 222 322 422 522
131 231 331 431 531 || 132 232 332 432 532
111 211 311 411 511 || 112 212 312 412 512
121 221 321 421 521 || 122 222 322 422 522
131 231 331 431 531 || 132 232 332 432 532
So, the total number of iterations (how many times you go through the loops) is the product of the loops. This one is a 3 * 5 * 2, so it will iterate 30 times.
因此,总迭代次数(经过循环的次数)是循环的乘积。这是一个 3 * 5 * 2,所以它会迭代 30 次。
回答by Stefan Kendall
The outer for "grabs" the inner for and iterates over IT. i.e., you have a for loop within a for loop. For each of i in the range 1..3, you must loop j from 1..3 also. As i becomes 2, j resets to 1 and the inner loop runs all over again.
外部 for “抓取”内部 for 并遍历 IT。即,您在 for 循环中有一个 for 循环。对于 1..3 范围内的每个 i,您还必须从 1..3 循环 j。当 i 变为 2 时,j 重置为 1 并且内部循环再次运行。
回答by Alan
You have a loop inside another loop.
你在另一个循环中有一个循环。
So for every iteration of the outer loop, you will run the inner loop to completion, starting at 1 ending at 3.
因此,对于外循环的每次迭代,您都将运行内循环直到完成,从 1 开始到 3 结束。
So you end up printing j=1,2,3, for each value of i. In this case i is 1,2, and 3.
所以你最终会为 i 的每个值打印 j=1,2,3。在这种情况下,我是 1,2 和 3。

