For-Each 循环
JDK 1.5 引进了一种新的循环类型,被称为 For-Each 循环或者加强型循环,它能在不使用下标的情况下遍历数组。
语法格式如下:
for(type element: array)
{
System.out.println(element);
}
我们可以用这个循环写一个比较大小的方法:就非常好用。
public static int Max(int x, int y) {
return x > y ? x : y;
}
public static int Max(int x, int y, int z) {
int m = Max(x, y);
return m > z ? m : z;
}
public static int Max(int a[]) {
int m = a[0];
for (int x : a)
m = m > x ? m : x;
return m;
}