4.4 增強for循環
-
jdk5引入,主要用於數組或集合的增強型for循環
-
語法:
for(聲明語句:表達式) { //代碼句子 }
-
聲明語句:聲明新的局部變量,該變量類型必須和數組的元素類型匹配。其作用域限定在循環語句塊,其值與此時數組元素的值相等
-
表達式:表達式是要訪問的數組名,或者是返回值為數組的方法
package com.kuangshen.struct; public class ForDemo05 { public static void main(String[] args) { int [] numbers = {10,20,30,40,50}; //遍歷數組的元素 for (int x:numbers){ System.out.println(x); } } }