免费预览已结束,剩余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年广东省河源市辅警考试题《公安基础知识》综合能力试题库附答案
- 2025年广东省辅警招聘《公安基础知识》真题及答案解析
- 山西省农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)及答案详解(基础+提升)
- 2026年北京市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(培优a卷)
- 山西省农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)含答案详解(轻巧夺冠)
- 2025年高校教师职业道德题库带答案(达标题)
- 湖州市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)及答案详解(典优)
- 齐齐哈尔市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)附答案详解(研优卷)
- 2025年云南省特种作人员取证培训以及特种设备作业人员取证培训考试继电保护作业复习题及答案
- 佳木斯市农村信用社联合社秋季校园招聘笔试备考题库(浓缩500题)附答案详解(培优a卷)
- 2025电梯安全管理员考试试题及答案
- 华为红线管理办法
- 2025年压力容器设计人员考核试题与答案
- 2025中国临床肿瘤学会CSCO结直肠癌诊疗指南解读课件
- 小班健康:控制甜食摄入教育
- 借学科之力敲劳动之门
- 污水处理站施工组织设计 (二)
- 肝功能衰竭的护理查房
- 工业设备保温隔热工程承包合同
- 放化疗相关口腔黏膜炎预防及处理中华护理学会团体标准
- 金螳螂造价管理制度
评论
0/150
提交评论