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

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.

主站蜘蛛池模板: 中超| 疏勒县| 玛纳斯县| 临湘市| 新营市| 林西县| 乌审旗| 南丹县| 偃师市| 原阳县| 瑞金市| 河南省| 金溪县| 林甸县| 德江县| 临清市| 蚌埠市| 都昌县| 灵石县| 五莲县| 龙陵县| 麻阳| 怀远县| 五华县| 同心县| 浮山县| 齐齐哈尔市| 个旧市| 剑阁县| 加查县| 台东县| 古蔺县| 施秉县| 泗水县| 宜春市| 仪征市| SHOW| 个旧市| 沅陵县| 禹城市| 彰化县|