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

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.

主站蜘蛛池模板: 宜良县| 光山县| 班戈县| 神池县| 陇西县| 宜黄县| 五指山市| 延边| 瑞金市| 彭州市| 密山市| 睢宁县| 乾安县| 察雅县| 台东县| 台东县| 师宗县| 乐至县| 伊金霍洛旗| 富宁县| 梅河口市| 万盛区| 临夏县| 交口县| 漠河县| 灌云县| 桦川县| 茂名市| 江安县| 古田县| 常熟市| 手游| 屯昌县| 昆明市| 聊城市| 鄂伦春自治旗| 吉木萨尔县| 和田县| 金乡县| 龙南县| 马山县|