增强for循环

增强的for循环是在传统的for循环中增加的强大的迭代功能的循环,是在jdk1.5之后提出来的。

基本语法格式:

for(type 变量名:集合变量名){……}                       //把迭代的变量放在括号中,冒号后面为集合名

其中:迭代变量必须在()中定义。集合变量可以是数组或实现了iterable接口的集合类。

应用实例模板:

public static<AnyType> void print(Collection<AnyType> coll) {
    for(AnyType item : coll)
    System.out.println(item);
}

应用范围及实例:增强的for循环(泛型)主要是在一维数组、二维数组和List中应用。

publicstatic void main(String[] args) {

      //1、一维数组(普通数组)中的使用

      int array[] = {1,2,3,4,5,6,7};

      //增强for循环

      for(int arritem : array){

         System.out.println(arritem);

      }

      //普通的for循环

      for(int i = 0; i < array.length; i++){

         System.out.println(array[i]);

      }
原文地址:https://www.cnblogs.com/xuhaojun/p/9164048.html