官术网_书友最值得收藏!

Applicability and examples

Iterators are popular in most programming languages these days. It is probably most widely used in Java, along with the collections package. It is also implemented at the language level when a collection is traversed with the following loop construction:

for (String item : strCollection)
System.out.println(item);

The iterator pattern can be implemented using the generics mechanism. This way, we can make sure we can avoid runtime errors generated by forced castings.

Good practice when implementing new containers and iterators in Java is to implement the existing java.util.Iterator<E> and java.util.Collection<E> classes. When we need aggregators with specific behaviors, we should also consider extending one of the classes that were implemented in the java.collection package instead of creating a new one.

主站蜘蛛池模板: 故城县| 申扎县| 大理市| 麻江县| 威信县| 郎溪县| 仲巴县| 洛扎县| 衡南县| 通化县| 台湾省| 刚察县| 东莞市| 苏州市| 武定县| 疏勒县| 阿瓦提县| 富宁县| 德令哈市| 登封市| 鄂伦春自治旗| 钦州市| 阿瓦提县| 托克托县| 洪湖市| 巩义市| 黎城县| 格尔木市| 阆中市| 蛟河市| 青铜峡市| 柘城县| 无锡市| 兖州市| 芒康县| 绥棱县| 进贤县| 延川县| 青河县| 武功县| 图木舒克市|