普通for遍历和增强for的一些区别
增强的for循环的底层使用迭代器来实现,所以它就与普通for循环有一些差异
- 增强for使用增强for循环的时候不能使用集合删除集合中的元素;
- 增强for循环不能使用迭代器中的方法,例如remove()方法删除元素;
- 与普通for循环的区别:增强For循环有遍历对象,普通for循环没有遍历对象;
对于实现了RandomAccess接口的集合类,推荐使用普通for,这种方式faster than Iterator.next
The RandomAccess interface identifies that a particular java.util.List implementation has fast random access. (A more accurate name for the interface would have been "FastRandomAccess.") This interface tries to define an imprecise concept: what exactly is fast? The documentation provides a simple guide: if repeated access using the List.get( ) method is faster than repeated access using the Iterator.next( ) method, then the List has fast random access. The two types of access are shown in the following code examples.
最后
以上就是淡然皮卡丘最近收集整理的关于Java 中增强for循环和普通for循环的区别的全部内容,更多相关Java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复