problem of the day
DESCRIPTION
Problem Of The Day. Decipher the following phrase: STANDS 0 _ 2 3 4 5 6 7 8 9. Problem Of The Day. Decipher the following phrase: STANDS 0 _ 2 3 4 5 6 7 8 9 I just knew that No one understands. CSC 212 – Data Structures. Lecture 30: Iterator and Iterable. List ADT. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/1.jpg)
Problem Of The Day
Decipher the following phrase:
STANDS0 _ 2 3 4 5 6 7 8 9
![Page 2: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/2.jpg)
Problem Of The Day
Decipher the following phrase:
STANDS0 _ 2 3 4 5 6 7 8 9
I just knew that
No one understands
![Page 3: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/3.jpg)
LECTURE 30:ITERATOR AND ITERABLE
CSC 212 – Data Structures
![Page 4: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/4.jpg)
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 indices for absolution
positioning Can only use relative positions in NODELIST
![Page 5: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/5.jpg)
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 indices for absolution
positioning Can only use relative positions in NODELIST
![Page 6: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/6.jpg)
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 indices for absolution
positioning Can only use relative positions in
NodeList
![Page 7: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/7.jpg)
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 indices for absolution
positioning Can only use relative positions in
NodeList
Must violate ADT to access List’s elements
![Page 8: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/8.jpg)
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 indices for absolution
positioning Can only use relative positions in
NodeList
Must violate ADT to access List’s elements
![Page 9: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/9.jpg)
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 any
COLLECTION
![Page 10: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/10.jpg)
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 any
COLLECTION
![Page 11: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/11.jpg)
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
Using Iterator
![Page 12: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/12.jpg)
package java.util;public interface Iterator<E> {
boolean hasNext();
E next() throws NoSuchElementException;
void remove() throws UnsupportedOperationException;
}
Iterator Interface
![Page 13: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/13.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
![Page 14: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/14.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
![Page 15: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/15.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
IteratorCursor 0
LIST
![Page 16: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/16.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
IteratorCursor 0
![Page 17: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/17.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
IteratorCursor 1
![Page 18: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/18.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
IteratorCursor 2
![Page 19: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/19.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
IteratorCursor 3
![Page 20: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/20.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
IteratorCursor ???
![Page 21: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/21.jpg)
Maintain a cursor showing where they work Value at cursor returned when next()
called Very implementation specific issues for
cursor To iterate over an INDEXLIST, cursor must
be index Cursor is POSITION for POSITIONLIST’s
ITERATOR
How Iterators Work
LIST
IteratorCursor 5
![Page 22: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/22.jpg)
Iterator for INDEXLIST
public class IndexListIterator<E> {
private IndexList<E> theList;private int cursor;
public IndexListIterator(IndexList<E> list) { theList = list; cursor = 0;}
public boolean hasNext() { return cursor != theList.size();}
// More goes here…
![Page 23: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/23.jpg)
Limit of Iterator
Defines limited set of methods Cannot add or change elements in
Collection Changes to data elsewhere invalidates 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
![Page 24: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/24.jpg)
POSITION with Benefits
cursor is next Position in PositionList
Needing to know class specifics avoided Rely on POSITIONLIST’s methods instead
head tail
elements
Iterator
Cursor
![Page 25: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/25.jpg)
POSITION with Benefits
cursor is next Position in PositionList
Needing to know class specifics avoided Rely on POSITIONLIST’s methods instead
head tail
elements
Iterator
Cursor
![Page 26: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/26.jpg)
POSITION with Benefits
cursor is next Position in PositionList
Needing to know class specifics avoided Rely on POSITIONLIST’s methods instead
head tail
elements
Iterator
Cursor
![Page 27: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/27.jpg)
POSITION with Benefits
cursor is next Position in PositionList
Needing to know class specifics avoided Rely on POSITIONLIST’s methods instead
head tail
elements
Iterator
Cursor
![Page 28: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/28.jpg)
POSITION with Benefits
cursor is next Position in PositionList
Needing to know class specifics avoided Rely on POSITIONLIST’s methods instead
head tail
elements
Iterator
•Cursor
![Page 29: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/29.jpg)
Iterator for POSIIONLIST
private PositionList<E> theList;private Position<E> cursor;
public boolean hasNext() { return cursor != null;}
public E next() throws NoSuchElementException { if (cursor == null) { throw new NoSuchElementException(); } E retVal = cursor.element(); if (cursor != theList.last()) { cursor = theList.next(cursor); } else { cursor = null; } return retVal;}
![Page 30: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/30.jpg)
ITERABLE
Why Should You Care?
![Page 31: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/31.jpg)
Iterable Interface
So simple makes Iterator look complex
![Page 32: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/32.jpg)
Iterable Interface
So simple makes Iterator look complex Get ready for this – it is really big!
![Page 33: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/33.jpg)
Iterable Interface
So simple makes Iterator look complex Get ready for this – it is really big!
Rocks your world and makes code easy to write
![Page 34: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/34.jpg)
Iterable Interface
So simple makes Iterator look complex Get ready for this – it is really big!
Rocks your world and makes code easy to write
Java’s prettiest feature relies on this interface
![Page 35: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/35.jpg)
Iterable Interface
So simple makes Iterator look complex Get ready for this – it is really big!
Rocks your world and makes code easy to write
Java’s prettiest feature relies on this interface
![Page 36: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/36.jpg)
Iterable Interface
So simple makes Iterator look complex Get ready for this – it is really big!
Rocks your world and makes code easy to write
Java’s prettiest feature relies on this interface
package java.lang;public interface Iterable<E> { public Iterator<E> iterator();}
![Page 37: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/37.jpg)
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 loopfor (Type variableName : IterableName)
IndexList<Integer> idx = …;for (Integer i : idx) { … }
![Page 38: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/38.jpg)
Integer findLargest(Iterable<Integer> able) {Integer retVal = Integer.MIN_VALUE; for (Integer datum : able) { if (datum > retVal) { retVal = datum; }}return retVal;
}
able could be LIST, HASHMAP, VERTEXSET…
For-Each Rocks The Hizzy
![Page 39: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/39.jpg)
Your Turn
Get into your groups and complete activity
![Page 40: Problem Of The Day](https://reader031.vdocuments.net/reader031/viewer/2022032805/568133f5550346895d9ae741/html5/thumbnails/40.jpg)
For Next Lecture
Read GT 6.4 before Friday’s lecture What if we want indices & 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 9 days