for each语法:
for(type l:o){
expression;
}
遍历一维数组:
int[] a={4,45,6,76,8};
for(int i:a){
System.out.print(i+" ");
}
->4 45 6 76 8
遍历二维数组:
int[][] a = {{1,2},{3,5},{9,8},{8,9},{1,2}};
//i现在为一维数组,即i[0]={1,2},i[1]={3,5}
for(int[] i:a){ //降维
//System.out.println(i[0]+" "+i[1]);
for(int j:i){
System.out.print(j+" ");
}
}
->1 2 3 5 9 8 8 9 1 2
本文深入讲解了Java中foreach循环的语法及应用,包括一维数组和二维数组的遍历方式,通过实例展示了如何使用foreach语法简化数组操作。
5176

被折叠的 条评论
为什么被折叠?



