示例 1 : 增强型for循环
package cm.po;
public class beifan {
public static void main(String[] args) {
int values [] = new int[]{18,62,68,82,65,9};
//常规遍历
for (int i = 0; i < values.length; i++) {
int each = values[i];
System.out.println(each);
}
//增强型for循环遍历
for (int each : values) {
System.out.println(each);
}
}
}
示例 2 : 用增强型for循环找出最大的那个数
package cm.po;
public class beifan {
public static void main(String[] args)
{
// TODO Auto-generated method stub
int temp=-1;
int []a=new int [4];
int i=0;
for( i=0;i<a.length;i++)
{
a [i]=(int)(Math.random()*10);
System.out.print(a[i]+" ");
}
System.out.println(" ");
for(int s:a)
{
if(s>temp)
{
temp=s;
}
}
System.out.println("最大值为"+temp);
}
}