



免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java IteratorTo generate successive elements from a series, we can use java iterator. It is an improvement over Enumeration interface. Iterator takes the place of Enumeration since jdk 1.2It is a nice utility for collections. Every collection is unique on its own and imagine if we have have to write logic on our own for every collection when there is a need to iterate it. Instead, java forces a collection to deliver an iterator.These nice utilities makes java lovable, isnt it?Important points to note: We can iterate only in one direction Iteration can be done only once. If you reach the end of series its done. If we need to iterate again we should get a new Iterator. Iterator Example without Genericspackage com.javapapers; import java.util.ArrayList; import java.util.Iterator; public class ExampleIterator public static void main(String args) ArrayList animal = new ArrayList(); animal.add(Horse); animal.add(Lion); animal.add(Tiger); Iterator animalItr = animal.iterator(); while(animalItr.hasNext() String animalObj = (String)animalItr.next(); System.out.println(animalObj); Without generics, Iterator returns the Object and we need to typecast it.Ads by GoogleIterator Example using Genericspackage com.javapapers; import java.util.ArrayList; public class ExampleIterator public static void main(String args) ArrayList animal = new ArrayList(); animal.add(Horse); animal.add(Lion); animal.add(Tiger); for(String animalObj : animal) System.out.println(animalObj); Output:HorseLionTigerLook how simple it is. There is no reference to the Iterator explicitly since we are using for-each of generics.Iterable and IteratorTo make an object iterable it needs to emit an Iterator object. To enforce this contract, Iterator interface is to be used. It contains a method named iterator() and it returns Iterator. Hence, any class that implements Iterable will return an Iterator.public interface Collection extends Iterable For example take any Collection. A Collection is an interface that represents container for series of elements. Every collections like ArrayList, Vector implements Collection and so Iterator. One advantage of Iterable is, when you implement Iterable then those object gets support for for:each loop syntax. Removing elements using Iterator Iterator has a remove method using which we can delete elements from the underlying object. It removes the last element returned by the iterator. Difference between Iterator and Enumeration interfaces1. remove() method is introduced in iterator. Using this method we can remove element from the underlying collection which we are iterating. 2. Enumeration has two methods and both are available in iterator. Method names for both of them are shortened. ListIterator an even better Iterator for a List containing more utility methods like getting index of elements and adding elements to the base object. Using ListIterator we can iterate in both the directions.ConcurrentModificationExceptionLook at the following code, it throws ConcurrentModificationException. We cannot add or remove elements to the underlying collection when we are using an iterator.Implementing our own Custom IteratorWe will create our own custom class and make it implement Iterable, so that it returns an Iterator using which we can iterate the elements.package com.javapapers; import java.util.ArrayList; public class ExampleIterator public static void main(String args) ArrayList animal = new ArrayList(); animal.add(Horse); animal.add(Lion); animal.add(Tiger); for(String animalObj : animal) System.out.println(animalObj); animal.add(Hyena); Output: Horse Exception in thread main java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at com.javapapers.ExampleIterator.main(ExampleIterator.java:13)package com.javapapers; import java.util.ArrayList; import java.util.Iterator; public class AnimalIterator implements Iterator private ArrayList animal; private int position; public AnimalIterator(Animal animalBase) this.animal = animalBase.getAnimal(); Overridepublic boolean hasNext() if (position animal.size() return true; elsereturn false; Overridepublic Object next() Object aniObj = animal.get(position); position+; return aniObj; Overridepublic void remove() animal.remove(position); package com.javapapers; import java.util.ArrayList; import java.util.Iterator; public class Animal implements Iterable private ArrayList animal = new ArrayList(); public Animal(ArrayList animal) this.animal = animal; public ArrayList getAnimal() return animal; Overridepublic Iterator iterator() return new AnimalIterator(this); package com.javapapers; import java.util.ArrayList; public class TestIterator public static void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 桐庐保洁知识培训班课件
- 2025年注册验船师考试(C级船舶检验专业基础安全)综合试题及答案一
- 幼儿园工作总结汇报七篇
- 2025年注册验船师资格考试(A级船舶检验专业综合能力)经典试题及答案一
- 贵商银行笔试题目及答案
- 2026届陕西省周至县第五中学化学高二上期末监测模拟试题含答案
- 2025年市场营销策略与实战案例分析笔试参考题
- 2025年裂化工艺优化考核题库
- 山东省枣庄市第十八中学2026届化学高二上期末调研试题含答案
- 2025年高级油气管道工程师岗位面试模拟题及解析
- 红楼梦之林黛玉
- 化学(基础模块)中职PPT完整全套教学课件
- 京东集团员工手册-京东
- 成人癌性疼痛护理-中华护理学会团体标准2019
- 初中语文学习方法指导
- 2023年苏州市星海实验中学小升初分班考试数学模拟试卷及答案解析
- GB/T 23483-2009建筑物围护结构传热系数及采暖供热量检测方法
- GB/T 22237-2008表面活性剂表面张力的测定
- 股指期权风险管理
- 《电业安全工作规程》
- 发证机关所在地区代码表
评论
0/150
提交评论