增强型 for循环
-
JAVA5 引入了一种主要用于数组或集合的增强型 for循环
-
JAVA 增强 for循环语法格式
for(声明语句 : 表达式) { //代码语句 } -
声明语句: 声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
-
表达式: 表达式是要访问的数组名,或者是返回值为数组的方法。
public class For_Demo_03 {
public static void main(String[] args) {
int[] int_01 = {10,20,30,40,50};//定义了一个数组
//遍历数组的元素
for (int x : int_01) {
System.out.println(x);
}
}
}
用普通 for循环表达的代码
public class For_Demo_03 {
public static void main(String[] args) {
int[] int_01 = {10,20,30,40,50};//定义了一个数组
for (int int_02 = 0;int_02 < 5;int_02++) {
System.out.println(int_01[int_02]);
}
}
}