Java 增强型的for循环 for each

For-Each循环

  For-Each循环也叫增强型的for循环,或者叫foreach循环。

  For-Each循环是JDK5.0的新特性(其他新特性比如泛型、自动装箱等)。

  For-Each循环的加入简化了集合的遍历。

其语法如下:

  for(type element: array)

  {

        System.out.println(element);

  }

例子

  其基本使用可以直接看代码:

  代码中首先对比了两种for循环;之后实现了用增强for循环遍历二维数组;最后采用三种方式遍历了一个List集合。

  

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; public class ForeachTest
{
public static void main(String[] args)
{
int[] arr = {1, 2, 3, 4, 5}; System.out.println("----------旧方式遍历------------");
//旧式方式
for(int i=0; i<arr.length; i++)
{
System.out.println(arr[i]);
} System.out.println("---------新方式遍历-------------"); //新式写法,增强的for循环
for(int element:arr)
{
System.out.println(element);
} System.out.println("---------遍历二维数组-------------"); //遍历二维数组 int[][] arr2 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} ; for(int[] row : arr2)
{
for(int element : row)
{
System.out.println(element);
}
} //以三种方式遍历集合List List<String> list = new ArrayList<String>(); list.add("a");
list.add("b");
list.add("c"); System.out.println("----------方式1-----------");
//第一种方式,普通for循环
for(int i = 0; i < list.size(); i++)
{
System.out.println(list.get(i)); } System.out.println("----------方式2-----------");
//第二种方式,使用迭代器
for(Iterator<String> iter = list.iterator(); iter.hasNext();)
{
System.out.println(iter.next());
}
System.out.println("----------方式3-----------");
//第三种方式,使用增强型的for循环
for(String str: list)
{
System.out.println(str); }
} }

  For-Each循环的缺点:丢掉了索引信息。

  当遍历集合或数组时,如果需要访问集合或数组的下标,那么最好使用旧式的方式来实现循环或遍历,而不要使用增强的for循环,因为它丢失了下标信息。

Java 增强型的for循环 for each的更多相关文章

  1. Java中的for循环——通过示例学习Java编程(9)

      作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=21 循环用于反复执行同一组语句,直到满足特定条件 ...

  2. Java流程控制:循环结构

    一.简介 顺序结构的程序语句只能被执行一次,如果您想要同样的操作执行多次,就需要使用循环结构. Java中有三种主要的循环结构: 'while'循环 'do...while'循环 'for'循环 在J ...

  3. JAVA中的for-each循环与迭代

    在学习java中的collection时注意到,collection层次的根接口Collection实现了Iterable<T>接口(位于java.lang包中),实现这个接口允许对象成为 ...

  4. java里如何实现循环打印出字符或字符数组里的内容

    不多说,直接上干货! java里如何实现循环打印出字符里的内容 没写,暂时不会 java里如何实现循环打印出字符数组里的内容 public class test { public static voi ...

  5. java里如何实现循环打印出字符串或字符串数组里的内容

    不多说,直接上干货! java里如何实现循环打印出字符串里的内容 思路:可以先将字符串转换成字符串数组. public class test { public static void main(Str ...

  6. Java中的do-while循环——通过示例学习Java编程(11)

    作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=22&cid=0 在上一篇教程中,我们讨论了w ...

  7. [转帖]java中的for循环

    java中的for循环 https://baijiahao.baidu.com/s?id=1621622990642364099&wfr=spider&for=pc 发现自己连 for ...

  8. java集合-遍历arraylist-for循环-从指定下标开始遍历-for的用法

    转载:http://www.9191boke.com/blogdetails/681220549.html java集合的for循环遍历有多种方式,但是都是从下标0开始遍历,有时会有从中间下标开始遍历 ...

  9. Java的三种循环:1、for循环 2、while循环 3、do...while循环

    Java的三种循环 Java三种循环结构: 1.for循环 2.while循环 3.do...while循环 循环结构组成部分:1.条件初始化语句,2.条件判断语句 , 3.循环体语句,4.条件控制语 ...

随机推荐

  1. android 判断字符串是否为空与比对["=="与equals()的区别]

    if (s == null || s.equals("")) ; } s.equals("")里面是要比对的字符串 声明字符串未赋初始值或值,然后比对就会出错, ...

  2. glibc-2.15编译error: linker with -z relro support required

    ./configure --prefix=/usr/local/glibc-2.15 configure: error: you must configure in a separate build ...

  3. 查看linux系统,服务,配置文件被修改的时间

    如何查看服务启动时间 [root@qike /]# ps -ef |grep nginx root 14730 1 0 16:45 ? 00:00:00 nginx: master process / ...

  4. hdu1247 Hat’s Words

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=1247 题目: Hat's Words Time Limit: 2000/1000 MS (Ja ...

  5. Vim插件管理——Vundle

    Vim插件管理--Vundle 都说Vim时程序员写给自己的编辑器,其中的情结可想而知.身为一只程序狗CodingDoge,今天就让我带各位学习Vim的使用. vim因为其庞大而强劲的插件受到无比的推 ...

  6. PHP执行定时任务

    PHP执行定时任务 1.当PHP像文件写入信息的时候 <?php for ($i=0; $i < 10; $i++) { $str="我是第".$i."条&q ...

  7. Maven系列二setting.xml 配置详解

    文件存放位置 全局配置: ${M2_HOME}/conf/settings.xml 用户配置: ${user.home}/.m2/settings.xml note:用户配置优先于全局配置.${use ...

  8. [转]ASP.NET MVC3 + EF 性能优化解决方案以及最优架构

    [集思广议]      我们用 asp.net mvc3 + ef 做了一个网站,现在是内测阶段,发现打开速度非常慢.首页打开(无缓存)都在5-6s以上(测试环境:程序和db都在本机),请问各位 mv ...

  9. [No000020]背单词提不起兴趣怎么办?

  10. 转: rapidJSON与jsoncpp语法说明

    转:  http://www.voidcn.com/blog/hudejun007/article/p-1811986.html