The principle of java for each loop

This article introducing the principle of java for each is very well written, turn it over and mark it.
http://blog.csdn.net/zhangyuan19880606/article/details/51241079

When compiling, the compiler will automatically convert the use of the for keyword into the use of the target iterator, which is the principle of the foreach loop. Furthermore, we come to two conclusions:

1. The reason why ArrayList can be traversed by foreach loop is because all Lists of ArrayList are sub-interfaces of Collection, and Collection is a sub-interface of Iterable, and the parent class of ArrayList, AbstractList, is correctly implemented The iterator method of the Iterable interface.

2. Any collection, whether provided by JDK or written by yourself, must correctly implement the Iterable interface as long as it wants to use the foreach loop to traverse.

In fact , this approach is the iterator pattern in the 23 design patterns.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326948724&siteId=291194637