lec09 - CSE 12 Implementing the Iterator Pattern Review of...

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

View Full Document Right Arrow Icon
09 Review of the Iterator Design Pattern Review of the Iterator and Iterable interfaces Implementing the Iterator interface The concurrent modification problem CSE 12 Implementing the Iterator Pattern
Background image of page 1

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

View Full DocumentRight Arrow Icon
The Iterator Software Design Pattern A common situation: A client needs to inspect the data elements in a collection, without wanting to know details of how the collection structures its data internally Solution: Define an interface that specifies how an iterator will behave (in the JCF, this is the Iterator<E> interface) Design the collection to be able to supply an object that implements that iterator interface (in the JCF, this behavior is required by the Iterable<E> interface) A client then can ask the collection for an iterator object, and use that iterator to inspect the collection’s elements, without having to know how the collection is implemented
Background image of page 2
Iterator<E> Interface The Iterator<E> interface is defined as follows: public interface Iterator<E> { public E next(); public boolean hasNext(); public void remove(); } So, any object that is-a Iterator<E> will have those operations as part of its API.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Iterable<E> Interface The Collection<E> interface extends the Iterable<E> interface, which is defined as follows: public interface Iterable<E> { public Iterator<E> iterator(); } So any class that implements Collection<E> must define an instance method iterator() that returns an Iterator<E> object for that instance
Background image of page 4
Using a Java Iterator A typical use of an Iterator for a Collection<String> c might be something like: // ask the Collection for an Iterator Iterator<String> i = c.iterator(); // then use the Iterator to visit the Collection’s data while ( i.hasNext() ) { String x = i.next(); // get the next String in c if ( x.length() > 10 ) { // and do something with it System.out.println(x + “ is longer than 10 chars”); } } (Here, the Collection c is referred to as the “backing collection” of the Iterator i )
Background image of page 5

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

View Full DocumentRight Arrow Icon
Using iterators Since Java 5, a new ‘foreach’ style syntax is available to even more conveniently write an iteration over the elements of any object (such as a Collection) that implements the Iterable interface. Again, suppose that c is a Collection<String> : for( String x: c ) { if ( x.length() > 10 ) { System.out.println(x + “ is longer than 10 chars”); } }
Background image of page 6
Iterator<E> method descriptions boolean hasNext () Returns: true if the iteration has more elements. (In other words, returns true if next would return an element rather than throwing an exception.) E next () Returns: the next element in the iteration. Throws:
Background image of page 7

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

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

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 24

lec09 - CSE 12 Implementing the Iterator Pattern Review of...

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

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