The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
The for
statement provides a compact way to iterate over a range of values.for
语句提供了一种对一系列值进行迭代的紧凑方法。Programmers often refer to it as the "for loop" because of the way in which it repeatedly loops until a particular condition is satisfied.程序员经常将其称为“for循环”,因为它重复循环直到满足特定条件的方式。The general form of the for
statement can be expressed as follows:for
语句的一般形式可以表示为:
for (initialization; termination; increment) { statement(s) }
When using this version of the 使用此版本的for
statement, keep in mind that:for
语句时,请记住:
false
, the loop terminates.false
时,循环终止。The following program, 以下程序ForDemo
, uses the general form of the for
statement to print the numbers 1 through 10 to standard output:ForDemo
使用for
语句的一般形式将数字1到10打印到标准输出:
class ForDemo { public static void main(String[] args){ for(int i=1; i<11; i++){ System.out.println("Count is: " + i); } } }
The output of this program is:该程序的输出为:
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10
Notice how the code declares a variable within the initialization expression.注意代码是如何在初始化表达式中声明变量的。The scope of this variable extends from its declaration to the end of the block governed by the 此变量的范围从其声明扩展到由for
statement, so it can be used in the termination and increment expressions as well.for
语句控制的块的末尾,因此它也可以用于终止表达式和增量表达式。If the variable that controls a 如果控制for
statement is not needed outside of the loop, it's best to declare the variable in the initialization expression.for
语句的变量不需要在循环之外,最好在初始化表达式中声明该变量。The names 名称i
, j
, and k
are often used to control for
loops; declaring them within the initialization expression limits their life span and reduces errors.i
、j
和k
通常用于控制循环;在初始化表达式中声明它们会限制它们的寿命并减少错误。
The three expressions of the for
loop are optional; an infinite loop can be created as follows:for
循环的三个表达式是可选的;可以按如下方式创建无限循环:
// infinite loop for ( ; ; ) { // your code goes here }
The for
statement also has another form designed for iteration through Collections and arrays.for
语句还有另一个形式,用于通过集合和数组进行迭代。This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.该表单有时被称为增强的for语句,可用于使循环更紧凑、更易于阅读。To demonstrate, consider the following array, which holds the numbers 1 through 10:为了演示,考虑下面的数组,它保持数字1到10:
int[] numbers = {1,2,3,4,5,6,7,8,9,10};
The following program, 以下程序EnhancedForDemo
, uses the enhanced for
to loop through the array:EnhancedForDemo
使用增强的for
在数组中循环:
class EnhancedForDemo { public static void main(String[] args){ int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers) { System.out.println("Count is: " + item); } } }
In this example, the variable 在本例中,变量item
holds the current value from the numbers array.item
保存数字数组中的当前值。The output from this program is the same as before:该程序的输出与之前相同:
Count is: 1 Count is: 2 Count is: 3 Count is: 4 Count is: 5 Count is: 6 Count is: 7 Count is: 8 Count is: 9 Count is: 10
We recommend using this form of the 我们建议尽可能使用这种形式的for
statement instead of the general form whenever possible.for
语句,而不是一般形式。