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 after an existing one Collection can remove any element it contains Loop over all elements without removing them List ADTs differ in how they provide access ArrayList’s indices give quick access to specific position Good at working at relative location with LinkedList
List ADT
Iterators Provides data access without knowing more As an ADT, methods independent of data storage Access elements 1-by-1 by calling next() method Like Comparator ; does not store data it uses Iterator class specific to instance holding data But means that code using Iterator independent Why is this important?
Iterators Provides data access without knowing more As an ADT, methods independent of data storage Access elements 1-by-1 by calling next() method Like Comparator ; does not store data it uses Iterator class specific to instance holding data But means that code using Iterator independent Why is this important? Breaks up code into separate class for each function Each part replaceable when better approach found
Perfect Example of Perfect Design
Iterator Interface package java.util; public interface Iterator { boolean hasNext(); E next() throws NoSuchElementException; void remove() throws UnsupportedOperationException; }
Iterator Needs a 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
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 Moves forward item-by-item only, cannot jump around
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 Moves forward item-by-item only, cannot jump around
Limits to Iterator Iterator ’s methods are very, very limited Iterator instance has no data; limits possible uses While iterating, can be tempting to modify data Requires actual instance, I TERATOR does not store data If underlying instance changes, results undefined May work, may crash, or may do both; caveat emptor
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(); }
Iterable Usage Occasionally use Iterator ; Iterable preferable Many ADT definitions include Iterable in definition Makes it possible to interchange ADT when processing Iterable support built-in to Java language Like Comparable, interface in java.lang package For-each loops through Iterable data directly Translates code to use Iterator (without extra code)
For-Each for the Win Slightly different loop than normal for loop for (type variableName : IterableVar) List idx; OrderedList profFirst; ArrayList whyNot; // Lots of code is here including assignments to our lists for (Integer i : idx ) { … } for (Costume cost : profFirst) { … } for (Scanner stupid : whyNot) { … }
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 A RRAY L IST & L INKED L IST are examples; many will follow
What Type Should Cursor Be?
Implementing Iterator Very implementation specific issues for cursor To iterate over an A RRAY L IST, cursor is index Cursor is node for L INKED 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
What Type Should Cursor Be?
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 Iterator (Start) public class ALIterator { private ArrayList theList; private int cursor; public ALIterator(ArrayList list) { theList = list; cursor = 0; } } public class LLIterator { private LinkedList theList; private DNode cursor; public LLIterator(LinkedList list) { theList = list; cursor = } }
Implementing Iterator (Start) public class ALIterator { private ArrayList theList; private int cursor; public ALIterator(ArrayList list) { theList = list; cursor = 0; } } public class LLIterator { private LinkedList theList; private DNode cursor; public LLIterator(LinkedList list) { theList = list; cursor = } }
Implementing the Iterator Iterator class needs access to List’s internal fields Implementationless ADT concept must be violated Code specific to implementation and not reusable All CSC212 design concepts get thrown out window
Implementing the Iterator Iterator class needs access to List’s internal fields Implementationless ADT concept must be violated Code specific to implementation and not reusable All CSC212 design concepts get thrown out window
Implementing for ArrayList L IST
Implementing for ArrayList L IST ALIterator cursor 0 theList
Implementing for ArrayList L IST ALIterator cursor 1 theList
Implementing for ArrayList L IST ALIterator cursor 2 theList
L IST Implementing for ArrayList ALIterator cursor 3 theList
L IST Implementing for ArrayList ALIterator
L IST Implementing for ArrayList ALIterator cursor 4 theList
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
Comparing Implementations ArrayList Iterator LinkedList Iterator Check if cursor valid: cursor != theList.size () Get element at cursor : theList.get ( cursor ) or array[cursor] Advancing cursor : cursor += 1 Check if cursor valid: cursor != null Get element at cursor : cursor.getElement () Advancing cursor : cursor = cursor. getNext ()
Your Turn Get into your groups and complete activity
For Next Lecture Read 8.1 – 8.4 before Friday’s lecture What is recursion & how does it work? What happens to locals if method called again? Why did we spend so much time on tracing methods? Week #11 assignment available on Angel No class on Monday No class on Monday; I will be out of town As a result, I am delaying quiz until Wednesday