foreach其实是for循环的一个简化版,foreach相当于for的子集.
例子
public class Repetition {
public static void main(String[] args) {
int arr[] = { 7, 10, 13 };
System.out.println("一维数组的元素分别是: ");
for (int x : arr) {
System.out.println(x);
}
}
}
运行结果为:
一维数组的元素分别是:
7
10
13