java用While循环自增运算遍历数组



java用While循环自增运算遍历数组实例。java遍历数组实例:

public class BirdArray {
public static void main(String args[]){
String[] str = new String[]{“麻雀”,”老鹰”,”白鸽”,”黄雀”,”百灵鸟”,”孔雀”,”鹦鹉”,”丹顶鹤”};
int index =0; //创建索引变量
System.out.println(“公园里有很多鸟,种类包括:”);
while(index<str.length){ //遍历数组
System.out.println(str[index++]); //自增索引值
}
}
}

结果输出:

run:
公园里有很多鸟,种类包括:
麻雀
老鹰
白鸽
黄雀
百灵鸟
孔雀
鹦鹉
丹顶鹤
BUILD SUCCESSFUL (total time: 0 seconds)

说明:创建索引变量index,用于指定数组的下标,随着索引的递增,while循环会逐步遍历每个元素并输出到控制台。要注意++index,与index++的区别。

++index: 会将index的值递增,然后再使用递增后的值。

index++: 首先使用index的值,然后再把变量的值递增。