Java foreach原理
从字节码可以看出,foreach使用了Iterator迭代器,循环判断hashNext(),用next()取操作对象。
Collection接口继承了Iterable接口,可以获取iterator对象。【Iterator
集合对象有对应的xxIterator,实现具体的hashNext()、next()、remove()等操作。
反编译源码更直观。
从字节码可以看出,foreach使用了Iterator迭代器,循环判断hashNext(),用next()取操作对象。
Collection接口继承了Iterable接口,可以获取iterator对象。【Iterator
集合对象有对应的xxIterator,实现具体的hashNext()、next()、remove()等操作。
反编译源码更直观。
Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.