java中的加强型for循环

public class HelloWorld {
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);
}

}

}
定义:一种功能很强的循环结构,可以用来一次处理数组中的元素(其他类型的元素集合也可以)最重要的是不用考虑下标值
for(variable:collection) statement
variable:用来存储数组中的每一个元素,并执行相应的语句(块)。
collection:必须是数组或者是一个实现了Iterable接口的类对象(数据列表ArrayList)。

猜你喜欢

转载自blog.csdn.net/weixin_44333359/article/details/85345728