java for循环
Java for loop is used to iterate over a range of values. We can use for loop to iterate over an array, a list, java set etc.
Java for循环用于迭代一系列值。 我们可以使用for循环遍历数组, 列表 , java集等。
Java for循环 (Java for loop)
There are three types of for loop in java.
Java中有三种类型的for循环。
- General for loop 一般循环
- for-each or enhanced for loop for-each或增强的for循环
- Java For loop with label 带标签的Java For循环
Let’s look into different type of java for loop example.
让我们看看不同类型的Java for循环示例。
Java for循环示例 (Java for loop example)
General for loop in java have following form.
Java中的常规for循环具有以下形式。
for (variable initialization; termination condition; increment/decrement operation) {
// statements to be executed
}
- “variable initialization” happens only once when for loop begins execution. “变量初始化”仅在for循环开始执行时发生一次。
- “termination condition” should result in boolean expression, if it returns
false
then for loop terminates. “终止条件”应产生布尔表达式,如果返回false
则for循环终止。 - “increment/decrement” operation is performed after each for loop execution. In most of the scenarios, it should lead towards the termination condition unless you want for loop to not terminate at all. 在每个for循环执行之后执行“递增/递减”操作。 在大多数情况下,除非您不希望for循环完全终止,否则它应导致终止条件。
Below image shows the flow chart of java for loop.

下图显示了Java for循环的流程图。
Suppose we want to print integers 5 to 10, in this case we can use basic for loop.
假设我们要打印5到10的整数,在这种情况下,我们可以使用basic for循环。
package com.journaldev.javaforloop;
public class JavaForLoop {
public static void main(String[] args) {
//print integers 5 to 10
for (int i=5; i<=10; i++) {
System.out.println("Java for loop example - " + i);
}
}
}
每个循环的Java (Java for each loop)
Java for each loop is also called enhanced for loop. We can use for each loop to iterate over array or collection elements. Java for each loop is the recommended way wherever it’s possible to use it. It’s very easy and compact to write.
每个循环的Java也称为增强for循环。 我们可以为每个循环使用迭代数组或集合元素。 推荐在每种可能的地方使用Java进行每个循环。 编写起来非常简单和紧凑。
package com.journaldev.javaforloop;
import java.util.ArrayList;
import java.util.List;
public class JavaForEachLoopExample {
public static void main(String[] args) {
int[] intArray = { 1, 2, 3, 4, 5 };
for (int i : intArray)
System.out.println("Java for each loop with array - " + i);
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
for (String f : fruits)
System.out.println("Java for each loop with collection - " + f);
}
}
Notice from above example is that if there is only one statement in the for loop, then we don’t need to put them inside curly braces {}.
从上面的示例中注意到,如果在for循环中只有一条语句,那么我们不需要将它们放在花括号{}中。
带标签的Java for循环 (Java for loop with label)
We can add a label to for loop, it’s useful with break and continue statements to get out of outer loop. Note that by default break and continue statements work with inner loop only. Here is an example of for loop with label and how it’s used with continue statement.
我们可以在for循环中添加标签,这对于break和continue语句摆脱外部循环很有用。 请注意,默认情况下break和continue语句仅适用于内部循环。 这是带有标签的for循环的示例,以及如何将其与continue语句一起使用。
int[][] intArr = { { 1, -2, 3 }, { 0, 3 }, { 1, 2, 5 }, { 9, 2, 5 } };
process: for (int i = 0; i < intArr.length; i++) {
boolean allPositive = true;
for (int j = 0; j < intArr[i].length; j++) {
if (intArr[i][j] < 0) {
allPositive = false;
continue process;
}
}
if (allPositive) {
// process the array
System.out.println("Processing " + Arrays.toString(intArr[i]));
}
allPositive = true;
}
That’s all about java for loop.
这就是关于java for循环的全部内容。
Reference: Oracle Documentation
参考: Oracle文档
java for循环