public class ShuZuBianLi_19
{
public static void main(String[] args)
{
/*
數組遍歷:
遍歷的第一種方式:for循環
遍歷的第二種方式:forEach循環,也稱為 增強for循環 。
遍歷的第三種方式:while循環 或者 do while循環
*/
//定義數組
int[] ages = new int[]{17,19,20,30};
//1、通過for循環方式遍歷數組
for(int i=0;i<ages.length;i++){
//根據索引號獲取數組中的元素
int age = ages[i];
//System.out.println("age:"+age);
}
/*
2、通過for循環方式遍歷數組
格式: for(數組中元素的類型 變量 : 需要被遍歷的數組){
//輸出變量
}
*/
for(int age : ages){
System.out.println("age:"+age);
}
//3、通過while循環方式遍歷數組
int i=0;
while(i<ages.length){
System.out.println("age:"+ages[i]);
//i自增
i++;
}
}
}