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
List ADT Collection which we can access all elements Add element before an existing one Return the Collection’s 3 rd element Loop over all elements without removing them L IST ADTs differ in how they provide access I NDEX L IST uses indices for absolution positioning Can only use relative positions in N ODE L IST
List ADT
Oops…
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 any Makes it easy to write code using any C OLLECTION
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 any Makes it easy to write code using any C OLLECTION
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 { boolean hasNext(); E next() throws NoSuchElementException; void remove() throws UnsupportedOperationException; }
Iterator Needs a Cursor
Implementing Iterator (Start) public class ILIterator { private IndexList theList; private int cursor; public ILIterator(IndexList list) { theList = list; cursor = 0; } } public class PLIterator { private PositionList theList; private Position cursor; public PLIterator(PositionList list) { theList = list; cursor = list.first(); // TODO: handle empty list correctly } }
Limits to Iterator I TERATOR ’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 I TERATOR But result undefined, if data changes, I TERATOR can crash
Implementing Iterator Very implementation specific issues for cursor To iterate over an I NDEX L IST, cursor is index Cursor is P OSITION for P OSITION L IST ’s I TERATOR Iterator’s methods always use similar algorithm General outline identical, since interface defines task But implementation changes since cursor use differs
Implementing Iterator
Same Algorithm, But... Algorithm next() if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retVal else throw new NoSuchElementException endif end
Same Algorithm, But... Algorithm next() if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retVal else throw new NoSuchElementException endif end
Implementing for IndexList L IST
Implementing for IndexList ILIterator cursor 0 theList L IST
Implementing for IndexList L IST ILIterator cursor 0 theList
Implementing for IndexList L IST ILIterator cursor 1 theList
Implementing for IndexList L IST ILIterator cursor 2 theList
Implementing for IndexList L IST ILIterator cursor 3 theList
Implementing for IndexList L IST ILIterator cursor ? theList
Implementing for IndexList L IST ILIterator cursor 5 theList
Implementing for PositionList cursor is next Position in PositionList Needing to know class specifics avoided Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList
Implementing for PositionList cursor is next Position in PositionList Needing to know class specifics avoided Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList
Implementing for PositionList cursor is next Position in PositionList Needing to know class specifics avoided Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList
Implementing for PositionList cursor is next Position in PositionList Needing to know class specifics avoided Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList
Implementing for PositionList cursor is next Position in PositionList Needing to know class specifics avoided Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList
Comparing Implementations IndexList Iterator PositionList Iterator Check if cursor valid: 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 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 { public Iterator iterator(); }
For-each for the Win Iterable support built-in to Java for free As is any class in the java.lang package For-each loops work with any Iterable class Uses Iterator to loop over each element in class Slightly different loop than normal for loop for (Type variableName : IterableName) IndexList idx = …; for (Integer i : idx) { … }
For-Each Rocks The Hizzy Integer findSum(Iterable able) { Integer retVal = 0; for (Integer datum : able) { retVal += datum; } return retVal; } able could be (almost) ANY C OLLECTION class I NDEX L IST & P OSITION L IST first 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 want indices & P OSITION s? Can we handle power to switch between the two? What implementation issues do S EQUENCE s cause? Week #11 assignment available on Angel Programming Assignment #2 Programming Assignment #2 due in 7 days