Java 简明教程
Java - for each Loop
Java for each Loop
for each 循环是一种特殊的 repetition control 结构,它允许您高效地编写需要执行特定次数的循环。
A for each loop is a special repetition control structure that allows you to efficiently write a loop that needs to be executed a specific number of times.
即使您不知道任务要重复多少次,for each 循环也很有用。
A for each loop is useful even when you do not know how many times a task is to be repeated.
Syntax
以下是增强 for loop(也称为 foreach 循环)的语法 −
Following is the syntax of enhanced for loop (or, for each loop) −
for(declaration : expression) {
// Statements
}
Execution Process
-
Declaration − The newly declared block variable, is of a type compatible with the elements of the array you are accessing. The variable will be available within the for block and its value would be the same as the current array element.
-
Expression − This evaluates to the array you need to loop through. The expression can be an array variable or method call that returns an array.
Examples
Example 1: Iterating Over a List of Integers
在此示例中,演示了使用 foreach 循环打印整数列表内容。此处将整数列表创建为数字,并初始化为一些值。然后,使用 foreach 循环打印每个数字。
In this example, we’re showing the use of a foreach loop to print contents of an List of Integers. Here we’re creating an List of integers as numbers and initialized it some values. Then using foreach loop, each number is printed.
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String args[]) {
List<Integer> numbers = Arrays.asList(10, 20, 30, 40, 50);
for(Integer x : numbers ) {
System.out.print( x );
System.out.print(",");
}
}
}
10, 20, 30, 40, 50,
Example 2: Iterating Over a List of Strings
在这个示例中,我们演示如何使用 foreach 循环来打印 List 的内容 String。此处我们创建一个字符串数组作为名称,并为其初始化一些值。然后使用 foreach 循环打印了每个名称。
In this example, we’re showing the use of a foreach loop to print contents of an List of String. Here we’re creating an array of Strings as names and initialized it some values. Then using foreach loop, each name is printed.
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String args[]) {
List<String> names = Arrays.asList("James", "Larry", "Tom", "Lacy");
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
}
}
James, Larry, Tom, Lacy,
Example 3: Iterating Over an Array of Objects
在这个示例中,我们演示如何使用 foreach 循环来打印 Student 数组的内容 Object。此处我们创建一个 Student 数组作为 Student 对象,并为其初始化一些值。然后使用 foreach 循环打印了每个名称。
In this example, we’re showing the use of a foreach loop to print contents of an array of Student Object. Here we’re creating an array of Students as Student object and initialized it some values. Then using foreach loop, each name is printed.
public class Test {
public static void main(String args[]) {
Student[] students = { new Student(1, "Julie"), new Student(3, "Adam"), new Student(2, "Robert") };
for( Students student : students ) {
System.out.print( student );
System.out.print(",");
}
}
}
class Student {
int rollNo;
String name;
Student(int rollNo, String name){
this.rollNo = rollNo;
this.name = name;
}
@Override
public String toString() {
return "[ " + this.rollNo + ", " + this.name + " ]";
}
}
[ 1, Julie ],[ 3, Adam ],[ 2, Robert ],