Unformatted text preview:

Iterator COMP 401 Spring 2013 Lecture 07 1 31 2013 Design Situa on Suppose we have an object that encapsulates some sort of collec on SongLibrary A collec on of songs in an iTunes like system PolygonModel A collec on of polygons in a 3D modeling system Polygon A collec on of points in our Assignment 2 Design Situa on Now suppose we have code outside of this collec on object that needs to examine each element of the underlying collec on in turn SongFilter A object that represents a set of search criteria that is to be applied to a collec on of songs An intersec on test in which each polygon of a PolygonModel needs to be evaluated Strategy 1 Provide access to underlying collec on as an array SongLibrary public Song getSongs PolygonModel public Polygon getPolygons Drawbacks May have to do a lot of work to create the array Collec on may be result of genera ve process Strategy 2 Provide index access to each underlying item in collec on SongLibrary public int getNumSongs public Song getSong int song idx PolygonModel public int getNumPolygons public Polygon getPolygon int polygon idx Drawbacks Doesn t help with genera ve collec ons Imposes restric ons on how collec on is represented and linearized Deteriorates encapsula on Strategy 3 Internalize a cursor SongLibrary public void resetSongCursor public Song getNextSong public boolean isCursorAtEnd Drawbacks Can t have two traversals going at the same me But this does come close Iterator Design Pa ern Provide a way to access the elements of an aggregate object sequen ally without exposing its underlying representa on Gang of Four Design Pa erns Consider for int i 0 i slist size i Song next song slist get i Do something with next song Iterator Design Pa ern Iterator object encapsulates details of item traversal Understands details of the underlying collec on Manages order of items May want a traversal that is not just rst to last Underlying collec on may not be linear Manages state of traversal Allows traversal to be picked up again later Assump on underlying collec on is not changed or modi ed while the traversal is occurring Interator should be able to detect this and signal an error Variant of pa ern will have iterator provide methods that modify underlying collec on safely Elements of Iterator Pa ern Collec on object is iterable Provides a method that returns an object that acts as an iterator Iterator object provides access to the elements in turn A method to test whether more items exist A method to retrieve the next item Java Iterator Pa ern Interfaces The Java Collec ons Framework de nes two generic interfaces for suppor ng the interable design pa ern Implemented by the various collec on types such as List E Map E Set E etc Iterable E Interator E iterator Iterator E Iterator E boolean hasNext Are we at the end of the traversal E next Get the next item of the traversal Throws a run me excep on if no next item void remove Not supported by all implementa ons Safely removes last item retrieved by next from the underlying collec on Iterable examples lec7 v1 Main1 Simple use Main2 Parallel iterators Main3 Simultaneous iterators Main4 for each syntac c sugar Main1 Visualized 1 ArrayList Song slist 0 Words and Guitar 1 Dig Me Out 2 Jenny 3 Li le Babies 4 Buy Her Candy Main1 Visualized 2 Iterator Song iter ArrayList Song slist list next idx 0 0 Words and Guitar 1 Dig Me Out 2 Jenny 3 Li le Babies 4 Buy Her Candy Main1 Visualized 3 Iterator Song iter ArrayList Song slist list next idx 0 public boolean hasNext if next idx list size return true return false NOTE This may or may not be how it is actually implemented but it is e ec2vely what is going on 0 Words and Guitar 1 Dig Me Out 2 Jenny 3 Li le Babies 4 Buy Her Candy Main1 Visualized 4 Iterator Song iter ArrayList Song slist list next idx 1 public Song next Song s list get next idx next idx return s NOTE Real implementa on would rst check to see if hasNext is s ll true and throw an excep on otherwise 0 Words and Guitar 1 Dig Me Out 2 Jenny 3 Li le Babies 4 Buy Her Candy lec7 v1 Main2 Parallel itera on Processes two di erent lists Iterator associated with each Iterators advance unevenly lec7 v1 Main3 Simultaneous itera on 2 Iterators 1 List Insert your own joke here lec7 v1 Main4 Java provides syntac c sugar for common use of iterators Supposing e coll is Iterable E then these are equivalent Iterator E iter e coll iterator while iter hasNext E elem iter next Do something with element for E elem e coll Do something with elem lec7 v2 A more complicated iterator Can build iterators that do things other than just go through every item Prior examples made use of Iterator E built into List E here we are going to implement our own specialized version of Iterator E


View Full Document

UNC-Chapel Hill COMP 401 - comp401sp13lec07Iterator

Documents in this Course
Objects

Objects

36 pages

Recursion

Recursion

45 pages

Load more
Loading Unlocking...
Login

Join to view comp401sp13lec07Iterator and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view comp401sp13lec07Iterator and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?