L5 updated

L5 updated - CSE 12 : Basic data structures and...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CSE 12 : Basic data structures and object-oriented design Jacob Whitehill jake@mplab.ucsd.edu Lecture Five 8 Aug 2011 Monday, August 8, 2011 More on interfaces. Monday, August 8, 2011 Review of Iterable and Iterator interfaes Recall: If a class X is to implement the Iterable interface, then it must implement a method called iterator() which returns an object of type Iterator . Iterator itself is an interface, not a class; hence, X.iterator() can return an object of any class that implements the Iterator interface. Monday, August 8, 2011 A class can implement the Iterator interface if it implements the following three methods: // Returns true if the iteration has more // elements. boolean hasNext (); // Returns the next element in the iteration. Object next (); // Removes from the underlying collection the // last element returned by the iterator // ( optional operation ). void remove (); In the case of the List12 interface: The Iterator returned by iterator() must support the remove() method. Iterator interface Monday, August 8, 2011 The user of the Iterator can call these methods whenever he/she wants, subject to the following constraints (as deFned in the Iterator interface) on remove() : This method can be called only once per call to next . remove() should throw an IllegalStateException if the next method has not yet been called, or the remove method has already been called after the last call to the next method. Iterator interface Monday, August 8, 2011 The Iterator interface also speciFes that the behavior of an iterator is unspeciFed if the underlying collection is modiFed while the iteration is in progress in any way other than by calling this method. Iterator interface Monday, August 8, 2011 Iterator interface The Iterator interface also speciFes that the behavior of an iterator is unspeciFed if the underlying collection is modiFed while the iteration is in progress in any way other than by calling this method. Modifcations in the case of DoublyLinkedList12 mean addToFront() , removeFront() , etc. -- anything that changes the contents of the list. Unspecifed means that you are absolved of any responsibility for maintaining correct functionality in the Iterator if the user modiFes the DoublyLinkedList12 while he/she is iterating over it. Monday, August 8, 2011 Interface as a contract An interface speciFcation serves as a contract between user and implementor of the interface. The method signatures specify to the user what each method does, and how it is called (i.e., parameters). The comments describe to the implementor what each method must do and what values to return. Monday, August 8, 2011 The comments may also prescribe to the user various constraints on how the methods are called, e.g., next() must be called before remove() ....
View Full Document

Page1 / 73

L5 updated - CSE 12 : Basic data structures and...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online