这篇介绍java for each原理的文章写的很棒,转过来marke一下。
http://blog.csdn.net/zhangyuan19880606/article/details/51241079
在编译的时候编译器会自动将对for这个关键字的使用转化为对目标的迭代器的使用,这就是foreach循环的原理。进而,我们再得出两个结论:
1、ArrayList之所以能使用foreach循环遍历,是因为ArrayList所有的List都是Collection的子接口,而Collection是Iterable的子接口,ArrayList的父类AbstractList正确地实现了Iterable接口的iterator方法。
2、任何一个集合,无论是JDK提供的还是自己写的,只要想使用foreach循环遍历,就必须正确地实现Iterable接口
实际上,这种做法就是23中设计模式中的迭代器模式。
分享到:
相关推荐
VB For each循环实例 VB For each循环实例
从Java5起,在Java中有了for-each循环,可以用来循环遍历collection和array。For each循环允许你在无需保持传统for循环中的索引,或在使用iterator /ListIterator时无需调用while循环中的hasNext()方法就能遍历...
for_each 返回值实例详解;
下面小编就为大家带来一篇浅谈java 增强型的for循环 for each。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
本文详解了JAVA中的for-each循环与迭代,是JS入门学习中的基础知识,也是java中的难点知识,需要的朋友可以参考下 。
主要介绍了Java for each实现机制代码原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
MATLAB工具箱大全-将多重循环转为单重循环-For-Each
下面是用一个传统的for循环遍历集合的例子:用传统的for循环遍历数组的做法如下:这些做法逗比while循环更好,但是它们并不完美。如果真的那么不幸,并且外部集
Learn Java for Android Development, Third Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the...
本文实例分析了js的for in循环和java里foreach循环的区别。分享给大家供大家参考。具体分析如下: js里的for in循环定义如下: 代码如下:for(var variable in obj) { … } obj可以是一个普通的js对象或者一个数组。...
java1.7-part1
Write a Java program called ContinueLoop.java that uses a for loop with the variable "count" and count 1 to 10.. Display "count=<count>" each time you loop through. Skip the display statement using ...
其实查看jQuery的源代码,发现each的代码很简单,但为什么性能和原生的for循环相差几十倍呢? jQuery的each的核心代码 for (; i < length; i++) { value = callback.call(obj[i], i, obj[i]); if (value === ...
Excel VBA运用,删除行,循环For each next,rows.count计算,添加指定数据等, 打开Excel启用宏,然后按快捷键Ctrl+E
By highlighting the features and comparative elements of each language, and building on the reader's existing knowledge of object-oriented programming, C++ for Java Programmers enables users to ...
ASP是目前学习网页动态程序最基本的程序语言之一。通过对本教程的学习实践,你能更快的掌握ASP在动态网页设计中的一些问题,同时为制作网站等提供最大的方便!
值得注意的是,Python的for循环不同于其他语言中的传统for循环,它更像是“for each”循环,直接遍历可迭代对象的元素,而不需要手动管理索引或迭代器。这种简洁的语法使得for循环在Python中非常常用且易于理解。 ...
Learn Java for Android Development teaches programmers of any skill level the essential Java language and foundational Java API skills that must be learned to improve the programmer’s s chances of ...
jdk1.5以上版本中新增加的特性描述,带有代码例子让你很清晰的明白jdk的 常量 、枚举、 for each、反射等各种新的特性,相信需要的你,一定满足。
Learn Java for Android Development, 3rd Edition, is an update of a strong selling book that now includes a primer on Android app development (in Chapter 1 and Appendix C, which is distributed in the ...