440 likes | 521 Views
Problem of the Day. Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z) . Problem of the Day. Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z) (x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z). Problem of the Day. Simplify this equation:
E N D
Problem of the Day • Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)
Problem of the Day • Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z) (x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z)
Problem of the Day • Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z) (x - a) * (x - b) * (x - c) * …*(x – x)*…*(x - z) (x - a) * (x - b) * (x - c) * …* 0 *…*(x - z) = 0
CSC 212 – Data Structures Lecture 31:Iterator and Iterable
List ADT • Collection which we can access all elements • Add element before an existing one • Return the Collection’s 3rd element • Loop over all elements without removing them • List ADTs differ in how they provide access • IndexListuses indices for absolution positioning • Can only use relative positions in NodeList
List ADT • Collection which we can access all elements • Add element before an existing one • Return the Collection’s 3rd element • Loop over all elements without removing them • List ADTs differ in how they provide access • IndexListuses indices for absolution positioning • Can only use relative positions in NodeList
List ADT • Collection which we can access all elements • Add element before an existing one • Return the Collection’s 3rd element • Loop over all elements without removing them • List ADTs differ in how they provide access • IndexList uses indicesfor absolution positioning • Can only use relative positions in NodeList
List ADT • Collection which we can access all elements • Add element before an existing one • Return the Collection’s 3rd element • Loop over all elements without removing them • List ADTs differ in how they provide access • IndexList uses indicesfor absolution positioning • Can only use relative positions in NodeList Must violate ADT to access List’s elements
Oops… • Collection which we can access all elements • Add element before an existing one • Return the Collection’s 3rd element • Loop over all elements without removing them • List ADTs differ in how they provide access • IndexList uses indicesfor absolution positioning • Can only use relative positions in NodeList Must violate ADT to access List’s elements
Iterators • Scans elements in a Collection • Initial use will return first element… • …then second element returned with next call… • …returns the third element next… • …and so on until it moves past the last element • Iterator instance returned by another ADT • Process data without hard-coding ADT into method • Makes it easy to write code using anyCollection
Iterators • Scans elements in a Collection • Initial use will return first element… • …then second element returned with next call… • …returns the third element next… • …and so on until it moves past the last element • Iterator instance returned by another ADT • Process data without hard-coding ADT into method • Makes it easy to write code using anyCollection
Using Iterator • Write loops using an Iterator • Iterator can be used to get data from anything • Combine structures’ elements using Iterator • Improves modularity • Classes work with anything providing an Iterator • Improves reuse • Ignore details of how to access elements within ADT • Very simple code leaves hard part to Iterator
Iterator Interface package java.util; public interface Iterator<E> {booleanhasNext();Enext() throws NoSuchElementException;void remove() throws UnsupportedOperationException; }
Iterator Needs a Cursor • Uses cursorlike the one in Eclipse, Word, PPT, … • Like in Eclipse: cursor marks location to be used • Data EXTERNALto Iterator, like normal cursor • When run, application uses and moves cursor around • Cursor not part of document and not found in file • Only matters when editing: cursor useful only with data
Implementing Iterator (Start) public class ILIterator<E> { privateIndexList<E>theList;privateintcursor;public ILIterator(IndexList<E> list){theList= list;cursor = 0; }} public class PLIterator<E> { privatePositionList<E>theList;privatePosition<E> cursor;public PLIterator(PositionList<E> list) {theList= list;cursor = list.first(); // TODO: handle empty list correctly}}
Limits to Iterator • Iterator’s cursor differs from cursor in Word. PPT, • Cannot add or modify data; (mostly) used to read data • Instance goes through data once, cursor only advances • Only moves forward item-by-item, cannot jump around • While iterating, can be tempting to modify data • Easy to do, as object being iterated over is not Iterator • But result undefined, if data changes, Iterator can crash
Implementing Iterator • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursor is index • Cursor isPosition for PositionList’s Iterator • Iterator’s methods always use similar algorithm • General outline identical, since interface defines task • But implementation changes since cursor use differs
Implementing Iterator • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator • Iterator’s methods always use similar algorithm • General outline identical, since interface defines task • But implementation changes since cursor use differs
Implementing Iterator • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator • Iterator’s methods always use similar algorithm • General outline identical, since interface defines task • But implementation changes since cursor use differs
Same Algorithm, But... Algorithmnext()ifhasNext()thenEretVal= value at cursor’s location Advance cursor to refer to next locationreturn retValelse throw new NoSuchElementExceptionendif end
Same Algorithm, But... Algorithmnext()ifhasNext()thenEretVal= value at cursor’s location Advance cursor to refer to next locationreturnretValelsethrow new NoSuchElementExceptionendif end
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next()called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for IndexList • Cursorafter iterated data to make tasks simpler • Value at cursor returned when next() called • Veryimplementation specific issues for cursor • To iterate over an IndexList, cursoris index(int) • Cursoris Positionfor PositionList’s Iterator List
Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail
Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail
Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail
Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail
Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail
Comparing Implementations IndexListIterator PositionListIterator • Check if cursorvalid: cursor!=theList.size() • Get element at cursor: theList.get(cursor) • Advancing cursor: cursor+= 1 • Check if cursor valid: cursor!=null • Get element at cursor: cursor.element() • Advancing cursor: cursor=theList.next(cursor)
Limit of Iterator • Interface provides remove(), but… • …implementing it is a royal pain in the • Support not required by the interface • Instead throw UnsupportedOperationException • Relying on remove()risky,since it is optional • When in doubt, skip it
Why Should You Care? Iterable
Iterable Interface • So simple makes Iterator look complex • Java’s prettiest feature relies on this interface
Iterable Interface • So simple makes Iterator look complex • Java’s prettiest feature relies on this interface
Iterable Interface • So simple makes Iterator look complex • Java’s prettiest feature relies on this interface package java.lang; public interface Iterable<E> {publicIterator<E> iterator(); }
For-each for the Win • Iterablesupport built-into Java for free • As is any class in the java.lang package • For-each loops workwith any Iterable class • Uses Iteratorto loop over each element in class • Slightly different loop than normal for loopfor (Type variableName: IterableName)IndexList<Integer>idx = …;for (Integeri :idx) { … }
For-Each Rocks The Hizzy IntegerfindSum(Iterable<Integer>able) {IntegerretVal=0;for (Integerdatum:able) {retVal+=datum;}return retVal; } • ablecould be (almost)ANY Collection class • IndexList & PositionListfirst examples; many follow
Your Turn • Get into your groups and complete activity
For Next Lecture • Read GT 6.4 before Monday’s lecture • What if we wantindices & Positions? • Can we handle power to switch between the two? • What implementation issues do Sequences cause? • Week #11 assignment available on Angel • Programming Assignment #2 due in 7 days