L14cs211sp07-6up - Announcements Iteration & Inner...

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

View Full Document Right Arrow Icon
Iteration & Inner Classes Lecture 14 CS211 – Spring 2007 2 Announcements Prelim tonight! 3 boolean linearSearch( Comparable[] a , Object v) { for ( int i = 0; i < a.length; i++ ) { if ( a[i] .compareTo(v) == 0) return true; } return false; } Recall: Linear Search y Relies on data being stored in a 1D array ± Will not work if data is stored in another data structure such as a 2D array, list, stack, queue, . .. y All linear search really needs is: ± Are there more elements to look at? ± If so, get me the next element 4 Goal: Generic Linear Search y Data is contained in some object y Object has an adapter that permits data to be enumerated in some order y Adapter has two buttons ± boolean hasNext() : are there more elements? ± Object next() : if so, give me a new element that has not been enumerated so far 4 22 234 -9 4 -9 22 Linear search 5 Linear Search y First version: ± Input was int[] , used == to compare elements y More generic version: ± Input was Comparable[] , used compareTo() y Is there a still more generic version that is independent of the data structure? ± For example, works even with Comparable[][] y In other words, how should we iterate ? ± Goal: perform some action on each item in a collection 6 Strategy I: Copy to an Array y Copy the entire collection into an array ± Then iterate over the array y Good ± Straightforward to implement y Bad ± Can involve a lot of copying ² A lazy method might be better y Alternate version: Provide an array-like interface ± numItems() ± getItem(int i) y Bad ± It can be expensive to determine the i th item ± It doesn’t always make sense to refer to the i th item in a collection
Background image of page 1

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

View Full DocumentRight Arrow Icon
7 Strategy II: Iteration-State as Part of Collection y The collection itself keeps track of iteration ± Implies need for methods equivalent to ² void resetIteration() ² boolean hasNext() ² Object getNext() y Bad ± Just one iteration active at a time ± Makes it hard to share the collection 8 Sharks and Remoras Data class is like shark Iterator implementation is like a remora A single shark must allow many remoras to hook to it 9 Strategy III: Iterator as a Separate Object y Create an Iterator object ± It maintains the state of the iteration y Java provides an interface ( java.util.Iterator ) for this purpose y Good ± Can have multiple iterator objects associated with one collection ± Standard interface for all iterations y Bad ± The iterator object has to know a lot about the internal structure of the collection ² We’ll see how to use inner classes to fix this Remora teeth 10 Iterator Interface y java.util.Iterator ± Linear search can be written once and for all using Iterator interface y Any data structure that wants to support iteration should provide an implementation of Iterator ± We look at three ways to implement Iterator ² Using a separate class ² Using an inner class ²
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 6

L14cs211sp07-6up - Announcements Iteration &amp; Inner...

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

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