增强的for循环

Java5引入了一种主要用于数组或集合的增强型for循环。

格式:

For(声明语句:表达式)

{

//代码

}

声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

表达式:表达式是要访问的数组名,或者是返回值为数组的方法。

package day13;

public class ForTest {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 6, 90, 10};
        for (int x : numbers) {
            System.out.println(x);
        }
    }
}

执行结果:

posted @ 2021-08-05 22:47  Eleanor123  阅读(30)  评论(0编辑  收藏  举报