lec07 - CSE 12 Implementing the Iterator Pattern The...

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

View Full Document Right Arrow Icon
07-1/23 The Collection and Iterable interfaces The Iterator Design Pattern The Iterator interface Defining Iterator classes Concurrent Modification 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
07-2/23 Java Collections Framework (JCF) The JCF is a collection of interfaces, abstract and concrete classes providing a standard set of collection (container) types. The Collection interface is root of much of the JCF interface inheritance hierarchy However Collection itself extends the Iterable interface
Background image of page 2
4-3/36 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 Iterator<E> is also an interface in the JCF…
Background image of page 3

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

View Full DocumentRight Arrow Icon
07-4/23 Collection<E> Required methods boolean contains(Object target) Returns true if this collection contains the target element. boolean containsAll(Collection<?> c) Returns true if this collection contains all of the elements in the collection c . boolean isEmpty() Returns true if this collection contains no elements. int size() Returns the number of elements stored in this collection. Iterator<E> iterator() Returns an iterator over the elements in this collection. (Inherited from Iterable<E>.) Object[] toArray() Returns an array containing all of the elements in this collection. <T> T[] toArray(T[] a) Returns an array containing all of the elements in this collection with runtime type T .
Background image of page 4
4-5/36 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. But what are these methods supposed to do?. ..
Background image of page 5

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

View Full DocumentRight Arrow Icon
07-6/23 Iterator<E> Interface Required Methods boolean hasNext() Returns true if the iteration has more elements. E next() Returns the next element in the iteration. Optional Methods ( All optional methods not fully supported by an implementing class must throw an UnsupportedOperationException .) void remove() Removes from the underlying collection the element returned by the last call to next() . Once an element as been removed, remove() cannot be called again until another call to next() has been made.
Background image of page 6
The Iterator Software Design Pattern Problem: Often, a client needs to traverse the elements in a collection, without wanting to know how the collection structures its data Solution: Provide an interface that specifies how iterators will behave Have the collection object be able to supply an object that implements that iterator interface
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 06/15/2011 for the course ECON 1 taught by Professor Aben during the Fall '07 term at City College of San Francisco.

Page1 / 24

lec07 - CSE 12 Implementing the Iterator Pattern The...

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