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

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.

主站蜘蛛池模板: 临汾市| 泸定县| 华安县| 静宁县| 河间市| 临猗县| 宝清县| 盱眙县| 蛟河市| 定边县| 南漳县| 阿鲁科尔沁旗| 中牟县| 高要市| 永丰县| 黄浦区| 佛坪县| 浦东新区| 屏东市| 井冈山市| 铜鼓县| 澜沧| 西峡县| 平利县| 宜春市| 防城港市| 丰镇市| 广东省| 邢台市| 正阳县| 韩城市| 柳林县| 安乡县| 板桥市| 井陉县| 瑞昌市| 安顺市| 乌什县| 新宾| 呼图壁县| 财经|