1 / 54

Unit 16 Iterator

Unit 16 Iterator. Summary prepared by Kirk Scott. An account of barley rations issued monthly to adults (30 or 40 pints) and children (20 pints) written in cuneiform on clay tablet, written in year 4 of King Urukagina ( circa 2350 BC), from Girsu , Iraq, British Museum , London.

felcia
Télécharger la présentation

Unit 16 Iterator

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Unit 16Iterator Summary prepared by Kirk Scott

  2. An account of barley rations issued monthly to adults (30 or 40 pints) and children (20 pints) written in cuneiform on clay tablet, written in year 4 of King Urukagina (circa 2350 BC), from Girsu, Iraq, British Museum, London

  3. Cuneiform Tablet

  4. iPad (1st generation), a tablet computer

  5. Design Patterns in JavaChapter 28Iterator Summary prepared by Kirk Scott

  6. The Introduction Before the Introduction • The chapter in the book is divided into 3 parts: • 1. Ordinary Iteration • 2. Thread-Safe Iteration • 3. Iterating over a Composite

  7. This unit covers point 1 only • Point 2, involving threads, is beyond the scope of this course • Point 3, involving composites, is also beyond the scope of this course • The Composite design pattern will be covered later in the course, but there is enough to worry about there without considering iteration

  8. Book Definition of Pattern • The intent of the Iterator pattern is to provide a way to access the elements of a collection sequentially.

  9. Ordinary Iteration • The initial block of overheads that follow is a repetition of something I show in CS 202 • It goes through simple for each loops and while loops with iteration on the ArrayList class

  10. 9.5 Syntax Associated with the ArrayList Class (CS 202)

  11. 9.5.1 Importing the Class • The ArrayList class has to be imported in order to be used in a program. • import java.util.ArrayList;

  12. 9.5.2 Angle Bracket Notation to Specify Element Types • In a program which uses a collection class, declarations with angle bracket notation specify what kinds of elements the collection contains. • The following line of code would declare and construct an ArrayList containing references to Cup7 objects. • ArrayList<Cup7> myCollection = new ArrayList<Cup7>();

  13. If it is desirable for the ArrayList to contain simple types like integers or doubles, the values should be stored in instances of the wrapper classes Integer and Double. • ArrayList<Double> myCollection = new ArrayList<Double>();

  14. 9.5.3 In For Each Loops Elements Cannot be Added to or Removed from a Collection • Traversing all of the elements of an ArrayList can be done with a “for each” loop. • In the for statement a local reference named someCup is declared. • The for statement also contains a colon followed by the name of the collection to be accessed. • The type of the local reference agrees with the type of the elements of the collection.

  15. The loop runs through all of the elements of myCollection, successively assigning references of its elements to the local reference, someCup. • The loop delivers access to successive elements of the collection, but it is not possible to call methods on the collection itself in the body of the loop. • This means that it is not possible to add elements to, or remove them from the collection when using this syntax.

  16. For each loop example: • for(Cup7 someCup: myCollection) • { • myterminal.println(someCup); • }

  17. 9.5.4 Iterators and Collections • Traversing the elements of a collection is known as iteration. • The Java collections framework includes interfaces named Iterator and Iterable. • They support the traversal of a collection where it is possible to alter the collection while processing it.

  18. The Iterable interface contains one method, named iterator(). • Calling this method on a collection class returns a reference to an iterator. • Angle bracket notation is used to specify what kinds of elements the iterator is dealing with.

  19. If myCollection contains Cup7 elements, then the declaration of an iterator for the collection would take this form: • Iterator<Cup7> myIterator = myCollection.iterator();

  20. 9.5.5 Iterator Methods • Three different methods can be called on an iterator. • boolean hasNext() • Returns true if the iteration has more elements.

  21. E next() • Returns the next element (of type E) in the iteration. • Calling this method repeatedly until the hasNext() method returns false will return each element in the underlying collection exactly once.

  22. void remove() • Removes from the underlying collection the last element returned by the iterator. • This method can be called only once per call to next(). • Notice that it’s called on the iterator, not on the underlying collection.

  23. 9.5.6 In While Loops with Iterators, Elements can be Added to or Removed from a Collection • Assume that myCollection contains references to the Cup7 class. • These can be put in by a call to a simple add() method on the ArrayList. • The iterator for the collection is obtained before the loop. • The test for continuing to loop is a call to hasNext().

  24. In the body of the loop, the call to next() returns a reference to the next element, and the call to remove() causes that element to be removed from the collection. • Example code is given on the next overhead. • This example removes from myCollection all elements which contain 0 or fewer seeds:

  25. import java.util.Iterator; • … • Iterator<Cup7> myIterator = myCollection.iterator(); • Cup7 someCup; • while(myIterator.hasNext()) • { • someCup = myIterator.next(); • if(someCup.getSeedCount() <= 0) • myIterator.remove(); • }

  26. 9.5.7 In For Loops with Iterators Elements can be Added to or Removed from a Collection • The first part of the for statement obtains the iterator for the collection. • The second part of the for statement causes the iteration to continue as long as there is another element in the collection • See the code on the next overhead. • Notice that there is a semicolon setting off a third part of the for statement, but the third part is not used.

  27. import java.util.Iterator; • … • for(Iterator<Cup7> myIterator = myCollection.iterator(); myIterator.hasNext(); ) • { • if((myIterator.next().getSeedCount()) <= 0) • myIterator.remove(); • }

  28. In Summary • The system supplies an Iterator class • This class has three methods: hasNext(), next(), and remove() • The system supplies an Iterable interface • A class that implements this interface has an iterator() method that returns an instance of Iterator on an instance of that class

  29. The point is that a Java collection class like ArrayList implements the Iterable interface • Therefore it is possible to acquire an iterator for an instance of ArrayList • This makes it possible do iteration over the ArrayList • This holds true for any of the collection classes in the Java API

  30. If you have already had CS 330, you may have created your own collection class (data structure with multiple elements) • You may also already have been required to take the steps necessary to make your class implement the Iterable interface • You may also have implemented a special case Iterator class

  31. The example which will follow may or may not agree with the presentation given in CS 330 • In any case, it illustrates applying the iterator idea to cups and seeds

  32. A Cup and Seed Example of Iteration • In this example the Cup class will extend the Java AbstractCollection class • In other words, the cup class, a container for seeds, will become an official collection class where the contents it holds are instances of seeds • In order to be a successful collection, the Cup class will have iterator(), size(), and add() methods

  33. The existence of the iterator() method implies that it will be possible, within the Cup class, to construct an iterator for a cup • This will require another class in the example: • CupIterator • This class will implement the Iterable interface • That means it will implement the hasNext(), next(), and remove() methods

  34. Example Code • Example code will be given next • The Seed class is given for reference, followed by the Cup class and the CupIterator class • As usual, going through the code in minute detail may not be very useful

  35. A couple of general observations can be made though: • This implementation essentially relies on the underlying ArrayList that the cup contains in order to hold seeds • Essentially, the methods needed in the Cup class are implemented by wrapping calls to corresponding ArrayList methods

  36. The same is true of the CupIterator class • It works with the ArrayList of seeds contained in the cup that it’s an iterator for • If you go to the Web folder for this example, you will also find code that makes use of these classes • That code will not be presented in these overheads • The class code follows

  37. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.awt.Graphics2D; • import java.awt.Rectangle; • import java.awt.geom.Ellipse2D; • import java.awt.geom.Point2D; • import java.lang.*; • import java.util.*; • import java.io.*; • public class Seed • { • private final Color seedColor = Color.BLUE; • private final int diameter = 6; • public Seed() • { • }

  38. public intgetDiameter() • { • return diameter; • } • public void drawSeed(Graphics2D g2, Point2D.Double locationPoint) • { • double xCoord = locationPoint.getX(); • double yCoord = locationPoint.getY(); • Ellipse2D.Double dot = new Ellipse2D.Double(xCoord, yCoord, diameter, diameter); • g2.setColor(seedColor); • g2.fill(dot); • g2.setColor(Color.BLACK); • } • }

  39. import java.awt.*; • import java.awt.event.*; • import javax.swing.*; • import java.awt.Graphics2D; • import java.awt.Rectangle; • import java.awt.geom.Ellipse2D; • import java.awt.geom.Point2D; • import java.lang.*; • import java.util.*; • import java.io.*; • /******* This declaration is correct. Because the Cup class extends a class • that implements the Iterable interface, the Cup class is by definition iterable. • *******/ • public class Cup extends AbstractCollection<Seed> • { • private Rectangle cupRectangle; • private ArrayList<Seed> seedList; • public Cup(intseedCountIn, intcupX, intcupY, intcupW, intcupH) • { • cupRectangle = new Rectangle(cupX, cupY, cupW, cupH); • seedList = new ArrayList<Seed>(); • for(inti = 0; i < seedCountIn; i++) • { • Seed aSeed = new Seed(); • seedList.add(aSeed); • } • }

  40. public Cup(intcupX, intcupY, intcupW, intcupH) • { • cupRectangle = new Rectangle(cupX, cupY, cupW, cupH); • seedList = new ArrayList<Seed>(); • } • public Rectangle getRectangle() • { • return cupRectangle; • }

  41. public Point2D.Double generateSeedCoordinates(Seed aSeed) • { • double seedX = 0; • double seedY = 0; • Random generator = new Random(); • if((aSeed.getDiameter() <= cupRectangle.getWidth()) • && (aSeed.getDiameter() <= cupRectangle.getHeight())) • { • seedX = (int) cupRectangle.getX() + • generator.nextInt((int) cupRectangle.getWidth() - • aSeed.getDiameter()); • seedY = (int) cupRectangle.getY() + • generator.nextInt((int) cupRectangle.getHeight() - • aSeed.getDiameter()); • } • else • { • seedX = cupRectangle.getX(); • seedY = cupRectangle.getY(); • } • return new Point2D.Double(seedX, seedY); • }

  42. public ArrayList<Seed> getCupArrayList() • { • return seedList; • } • public Iterator<Seed> iterator() • { • return new CupIterator(this); • } • public int size() • { • return seedList.size(); • } • public boolean add(Seed seedIn) • { • return seedList.add(seedIn); • } • }

  43. import java.util.*; • /******* This declaration is correct. It is the iterator class that implements the Iterator interface. The type of object that can be iterated over can be declared. • *******/ • public class CupIterator implements Iterator<Seed> • { • ArrayList<Seed> cupArrayList; • intarrayListIndex; • int state; • public CupIterator(Cup cupIn) • { • cupArrayList = cupIn.getCupArrayList(); • arrayListIndex = -1; • state = -1; • } • public booleanhasNext() • { • try • { • //System.out.println("hasNext(), arrayListIndex test value: " + (arrayListIndex + 1)); • cupArrayList.get(arrayListIndex + 1); • return true; • } • catch(IndexOutOfBoundsException e) • { • return false; • } • }

  44. public Seed next() throws NoSuchElementException • { • if((arrayListIndex < cupArrayList.size() - 1) && (cupArrayList.size() > 0)) • { • arrayListIndex++; • //System.out.println("next(), arrayListIndex value after incrementing: " + arrayListIndex); • Seed seedRef = cupArrayList.get(arrayListIndex); • state = 1; • return seedRef; • } • else • throw new NoSuchElementException(); • }

  45. public void remove() throws IllegalStateException • { • if(state == 1) • { • //System.out.println("remove(), arrayListIndex: " + arrayListIndex); • cupArrayList.remove(arrayListIndex); • arrayListIndex--; • state = 0; • } • else • throw new IllegalStateException(); • } • }

  46. Thread-Safe Iteration • This is beyond the scope of this course • No doubt it is a fascinating topic, full of tricks and traps, but if you don’t know what threads are, it’s pointless to talk about iteration in that context

  47. Iterating over a Composite • This is the part of the chapter where the book develops some code, making the classes of the Composite design pattern implement the Iterable interface • This means also developing a hierarchy of Iterator classes for components, leaves, and composites • This is relatively complex and will not be pursued further

  48. Lasater’s UML for the Pattern • Lasater’s UML diagram is given on the following overhead • I find it a little hard to understand • Could it be that the arrowheads are reversed? • Shouldn’t the client have references to the Aggregate (Collection) and the Iterator, not the other way around?

  49. UML for the Design Pattern • Looking at Lasater’s UML diagram, it seemed that maybe the fundamental idea could be made clearer with a simpler picture • My attempt is on the overhead following the next one • The first thing I’m trying to indicate in the diagram is that a client has a reference to, or makes use of an instance of a Collection class

More Related