首页 > 其他 > 详细

增强for循环

时间:2018-06-10 19:53:22      阅读:169      评论:0      收藏:0      [点我收藏+]

增强的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]);

      }

 

增强for循环

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!