第一部分:

For-each Loop

Purpose

The basic for loop was extended in Java5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each (because it is called this in other programming languages). I've also heard it called the for-in loop.

Use it in preference to the standard for loop if applicable (see last section below) because it's much more readable.

Series of values. The for-each loop is used to access each successive value in a collection of values.

Arrays and Collections. It's commonly used to iterate over an array or a Collections class (eg, ArrayList).

Iterable<E>. It can also iterate over anything that implements the Iterable<E> interface (must defineiterator() method). Many of the Collections classes (eg, ArrayList) implement Iterable<E>, which makes thefor-each loop very useful. You can also implement Iterable<E> for your own data structures.

General Form

The for-each and equivalent for statements have these forms. The two basic equivalent forms are given, depending one whether it is an array or an Iterable that is being traversed. In both cases an extra variable is required, an index for the array and an iterator for the collection.

这里我们只要知道下面的事实就好了:

  1. For-each语法内部,对collection是用nested iteratoration来实现的,对数组是用下标遍历来实现。
  2. Java 5 及以上的编译器隐藏了基于iteration和下标遍历的内部实现。(注意,这里说的是“Java编译器”或Java语言对其实现做了隐藏,而不是某段Java代码对其实现做了隐藏,也就是说,我们在任何一段JDK的Java代码中都找不到这里被隐藏的实现。这里的实现,隐藏在了Java 编译器中,我们可能只能像这篇帖子中说的那样,查看一段For-each的Java代码编译成的字节码,从中揣测它到底是怎么实现的了)

下面对“For-each”和“其对等的iteration/index实现”的对比再简洁明了不过了。

For-each loop Equivalent for loop
for (type var : arr) {
body-of-loop
}
for (int i = 0; i < arr.length; i++) {
type var = arr[i];
body-of-loop
}
for (type var : coll) {
body-of-loop
}
for (Iterator<type> iter = coll.iterator(); iter.hasNext(); ) {
type var = iter.next();
body-of-loop
}

Example - Adding all elements of an array

Here is a loop written as both a for-each loop and a basic for loop.

double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (double d : ar) { // d gets successively each value in ar.
sum += d;
}

And here is the same loop using the basic for. It requires an extra iteration variable.

double[] ar = {1.2, 3.0, 0.8};
int sum = 0;
for (int i = 0; i < ar.length; i++) { // i indexes each element successively.
sum += ar[i];
}

Where the for-each is appropriate

一定要注意For-each不是万能的,下面的场合是不适宜使用For-each的

Altho the enhanced for loop can make code much clearer, it can't be used in some common situations.

使用For-each时对collection或数组中的元素不能做赋值操作

  • Only access. Elements can not be assigned to, eg, not to increment each element in a collection.

同时只能遍历一个collection或数组,不能同时遍历多余一个collection或数组

  • Only single structure. It's not possible to traverse two structures at once, eg, to compare two arrays.

遍历过程中,collection或数组中同时只有一个元素可见,即只有“当前遍历到的元素”可见,而前一个或后一个元素是不可见的。

  • Only single element. Use only for single element access, eg, not to compare successive elements.

只能正向遍历,不能反向遍历(相比之下,C++ STL中还有reverse_iterator, rbegin(), rend()之类的东西,可以反向遍历)

  • Only forward. It's possible to iterate only forward by single steps.

如果要兼容Java 5之前的Java版本,就不能使用For-each

    • At least Java 5. Don't use it if you need compatibility with versions before Java 5.

此文章来自:http://blog.csdn.net/yasi_xi/article/details/25482173#t1

第二部分:

     

先看一下这么一段代码:

public static void main(String[] args)
{
List<String> list = new ArrayList<String>();
list.add("111");
list.add("222"); for (String str : list)
{
System.out.println(str);
}
}

用foreach循环去遍历这个list,结果就不说了,都知道。看一下Java是如何处理这个foreach循环的,javap反编译一下:

d:\MyEclipse\TestArticle\bin\test29>javap -verbose TestMain.class

反编译出来的内容很多,有类信息、符号引用、字节码信息,截取一段信息:

 1   public static void main(java.lang.String[]);
2 flags: ACC_PUBLIC, ACC_STATIC
3 Code:
4 stack=2, locals=4, args_size=1
5 0: new #16 // class java/util/ArrayList
6 3: dup
7 4: invokespecial #18 // Method java/util/ArrayList."<in
8 it>":()V
9 7: astore_1
10 8: aload_1
11 9: ldc #19 // String 111
12 11: invokeinterface #21, 2 // InterfaceMethod java/util/List.
13 add:(Ljava/lang/Object;)Z
14 16: pop
15 17: aload_1
16 18: ldc #27 // String 222
17 20: invokeinterface #21, 2 // InterfaceMethod java/util/List.
18 add:(Ljava/lang/Object;)Z
19 25: pop
20 26: aload_1
21 27: invokeinterface #29, 1 // InterfaceMethod java/util/List.
22 iterator:()Ljava/util/Iterator;

看不懂没关系,new、dup、invokespecial这些本来就是字节码指令表内定义的指令,虚拟机会根据这些指令去执行指定的C++代码,完成每个指令的功能。关键看到21、22这两行就可以了,看到了一个iterator,所以得出结论:在编译的时候编译器会自动将对for这个关键字的使用转化为对目标的迭代器的使用,这就是foreach循环的原理。进而,我们再得出两个结论:

1、ArrayList之所以能使用foreach循环遍历,是因为ArrayList所有的List都是Collection的子接口,而Collection是Iterable的子接口,ArrayList的父类AbstractList正确地实现了Iterable接口的iterator方法。之前我自己写的ArrayList用foreach循环直接报空指针异常是因为我自己写的ArrayList并没有实现Iterable接口

2、任何一个集合,无论是JDK提供的还是自己写的,只要想使用foreach循环遍历,就必须正确地实现Iterable接口

实际上,这种做法就是23中设计模式中的迭代器模式

数组呢?

上面的讲完了,好理解,但是不知道大家有没有疑问,至少我是有一个疑问的:数组并没有实现Iterable接口啊,为什么数组也可以用foreach循环遍历呢?先给一段代码,再反编译:

public static void main(String[] args)
{
int[] ints = {1,2,3,4,5}; for (int i : ints)
System.out.println(i);
}

同样反编译一下,看一下关键的信息:

 1          0: iconst_2
2 1: newarray int
3 3: dup
4 4: iconst_0
5 5: iconst_1
6 6: iastore
7 7: dup
8 8: iconst_1
9 9: iconst_2
10 10: iastore
11 11: astore_1
12 12: aload_1
13 13: dup
14 14: astore 5
15 16: arraylength
16 17: istore 4
17 19: iconst_0
18 20: istore_3
19 21: goto 39
20 24: aload 5
21 26: iload_3
22 27: iaload
23 28: istore_2
24 29: getstatic #16 // Field java/lang/System.out:Ljav
25 a/io/PrintStream;
26 32: iload_2
27 33: invokevirtual #22 // Method java/io/PrintStream.prin
28 tln:(I)V
29 36: iinc 3, 1
30 39: iload_3
31 40: iload 4
32 42: if_icmplt 24
33 45: return

这是完整的这段main函数对应的45个字节码指令,因为这涉及一些压栈、出栈、推送等一些计算机原理性的内容且对于这些字节码指令的知识的理解需要一些C++的知识,所以就不解释了。简单对照字节码指令表之后,我个人对于这45个字节码的理解是Java将对于数组的foreach循环转换为对于这个数组每一个的循环引用

第二部分内容来自:http://www.cnblogs.com/xrq730/p/4868465.html

Java:foreach实现原理的更多相关文章

  1. java foreach 循环原理

    java foreach 语法是在jdk1.5时加入的新特性,主要是当作for语法的一个增强,那么它的底层到底是怎么实现的呢?因为面试时被问到,所以在这边做一个记录. 首先来看看foreach能够使用 ...

  2. java foreach实现原理

    在平时Java程序中,应用比较多的就是对Collection集合类的foreach遍历,foreach之所以能工作,是因为这些集合类都实现了Iterable接口,该接口中定义了Iterator迭代器的 ...

  3. Java语法糖1:可变长度参数以及foreach循环原理

    语法糖 接下来几篇文章要开启一个Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的 ...

  4. java语法糖:(1)可变长度参数以及foreach循环原理

    语法糖 语法糖:是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的字节码或者特定的方式对这些语法做一些处理,开发者就可以直接方便地使用 ...

  5. for-each 循环原理

    for-each 循环原理1,for-each 是在java5 之后出现的.for是java 上的一个关键字,在jdk 找不到任何for的底层实现的.是因为for的底层实现被封装到了编译器中.所以通过 ...

  6. 浅谈 foreach 的原理

    package com.shenzhou; import java.util.ArrayList; import java.util.Iterator; import java.util.List; ...

  7. Java并发编程原理与实战五:创建线程的多种方式

    一.继承Thread类 public class Demo1 extends Thread { public Demo1(String name) { super(name); } @Override ...

  8. 可变长度参数以及foreach循环原理

    语法糖 接下来几篇文章要开启一个Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的 ...

  9. 了解一下Java SPI的原理

    了解一下Java SPI的原理 1 为什么写这篇文章? 近期,本人在学习dubbo相关的知识,但是在dubbo官网中有提到Java的 SPI,这个名词之前未接触过,所以就去看了看,感觉还是有很多地方有 ...

  10. 深入Java核心 Java内存分配原理精讲

    深入Java核心 Java内存分配原理精讲 栈.堆.常量池虽同属Java内存分配时操作的区域,但其适用范围和功用却大不相同.本文将深入Java核心,详细讲解Java内存分配方面的知识. Java内存分 ...

随机推荐

  1. 物化视图SQL

    物化视图SQL,如果没有结果集,就证明数据库中不存在物化视图 select a.owner,'' column_name,a.table_name,b.segment_name,b.segment_t ...

  2. 当通过Nuget包管理器获取还原组时,出现 提示 “xxxxx”已拥有为“xxxxx”定义的依赖项

    当通过Nuget包管理器获取还原组件时,出现  提示 “xxxxx”已拥有为“xxxxx”定义的依赖项 时 解决方法: 工具---扩展和更新,把Nuget包管理器卸载后,重启VS,再安装,现打开VS项 ...

  3. vue里面axios使用post

    let params = new URLSearchParams(); params.append('action', "login"); params.append('user' ...

  4. python3:实现字符串的全排列(无重复字符)

    最近在学一些基础的算法,发现我的数学功底太差劲了,特别是大学的这一部分,概率论.线性代数.高数等等,这些大学学的我是忘得一干二净(我当时学的时候也不见得真的懂),导致现在学习算法,非常的吃力.唉!不说 ...

  5. !!!常用CSS代码块

    图片排满一行.左右两端无间隙. <style type="text/css"> .img_abc{float:left;width:30%;margin-left:5% ...

  6. leetcode105

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  7. windows下Mysql8.0.12安装详解

    MySQL的安装过程还是比较繁琐,为了以后安装节约时间,将其详细安装过程总结如下: 1>下载对应版本 下载地址:https://dev.mysql.com/downloads/mysql/ 2& ...

  8. 本学期c#学习总结

    本学期c#学习总结 时间转瞬即逝,大一上半学期的学习生涯已经结束.虽然以前我没什么关于学习计算机的基础,但是经过了这几个月的学习我也还是有点收获的. 我发现c#语言的关键词有很多语言特性和固定的用法, ...

  9. Java的学习05

    今天学习了,Java中的LinkedList类.这个类需要用到链表的知识,以前一直以为,只有c/c++有链表.今天才知道,原来其他语言.也有链表,而且还是双向链表. /** * 自定义一个链表 * @ ...

  10. android 字体加粗

    参考 https://blog.csdn.net/to_cm/article/details/6002812 TextView tv = (TextView)findViewById(R.id.Tex ...