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
LECTURE 30:ITERATOR AND ITERABLE
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
Collection which we can access all elements Add element before 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
Collection which we can access all elements Add element before 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
Collection which we can access all elements Add element before 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
Must violate ADT to access List’s elements
List ADT
Collection which we can access all elements Add element before 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 Good at working at relative location with
LinkedList
Must violate ADT to access List’s elements
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
Example of Perfect Design
Iterator Interface
package java.util;public interface Iterator<E> {
boolean hasNext();
E next() throws NoSuchElementException;
void remove() throws UnsupportedOperationException;
}
Iterator Needs a Cursor
Uses cursor like the one in Eclipse, Word, PPT, … Like in Eclipse: cursor marks location to be
used Data EXTERNAL to 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
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
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 Moves forward item-by-item only, cannot jump
around
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 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, ITERATOR 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
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> { public Iterator<E> 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 loopfor (type variableName : IterableVar)
List<Integer> idx;OrderedList<Costume> profFirst;ArrayList<Scanner> 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<Integer> able) {Integer retVal = 0; for (Integer datum : able) { retVal += datum; }return retVal;
}
able could be (almost) ANY COLLECTION class ARRAYLIST & LINKEDLIST are examples;
many will follow
What Type Should Cursor Be?
What Type Should Cursor Be?
Implementing Iterator
Very implementation specific issues for cursor To iterate over an ARRAYLIST, cursor is index Cursor is node for LINKEDLIST’s ITERATOR
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 retValelse throw new NoSuchElementExceptionendif
end
Same Algorithm, But...
Algorithm next()if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retValelse throw new NoSuchElementExceptionendif
end
Implementing Iterator (Start)public class ALIterator<E> { private ArrayList<E> theList; private int cursor; public ALIterator(ArrayList<E> list) { theList = list; cursor = 0; }}
public class LLIterator<E> { private LinkedList<E> theList; private DNode<E> cursor; public LLIterator(LinkedList<E> list) { theList = list; cursor = }}
Implementing Iterator (Start)public class ALIterator<E> { private ArrayList<E> theList; private int cursor; public ALIterator(ArrayList<E> list) { theList = list; cursor = 0; }}
public class LLIterator<E> { private LinkedList<E> theList; private DNode<E> cursor; public LLIterator(LinkedList<E> 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
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
ALIteratorcursor 0theList
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
ALIteratorcursor 1theList
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
LIST
ALIteratorcursor 2theList
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
ALIteratorcursor 3theList
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
ALIteratorcursor ?theList
LIST
Implementing for ArrayList
Cursor after iterated data to make tasks simpler When next() called, value at index of cursor
returned Very implementation specific issues for
cursor To iterate over an ARRAYLIST, cursor is
index (int) Cursor is NODE for LINKEDLIST’s ITERATOR
ALIteratorcursor 4theList
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) orarray[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; I will be out of town As a result, I am delaying quiz until
Wednesday