1 / 44

Problem of the Day

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:

reed
Download Presentation

Problem of the Day

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Problem of the Day • Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

  2. 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)

  3. 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

  4. CSC 212 – Data Structures Lecture 31:Iterator and Iterable

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. Iterator Interface package java.util; public interface Iterator<E> {booleanhasNext();Enext() throws NoSuchElementException;void remove() throws UnsupportedOperationException; }

  14. 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

  15. 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}}

  16. 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

  17. 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

  18. 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

  19. 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

  20. Same Algorithm, But... Algorithmnext()ifhasNext()thenEretVal= value at cursor’s location Advance cursor to refer to next locationreturn retValelse throw new NoSuchElementExceptionendif end

  21. Same Algorithm, But... Algorithmnext()ifhasNext()thenEretVal= value at cursor’s location Advance cursor to refer to next locationreturnretValelsethrow new NoSuchElementExceptionendif end

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail

  31. Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail

  32. Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail

  33. Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail

  34. Implementing for PositionList • cursoris next Positionin PositionList • Needing to know class specifics avoided • Rely on PositionList’s methods instead head tail

  35. 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)

  36. 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

  37. Why Should You Care? Iterable

  38. Iterable Interface • So simple makes Iterator look complex • Java’s prettiest feature relies on this interface

  39. Iterable Interface • So simple makes Iterator look complex • Java’s prettiest feature relies on this interface

  40. 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(); }

  41. 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) { … }

  42. 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

  43. Your Turn • Get into your groups and complete activity

  44. 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

More Related