problem of the day

41
Problem Of The Day Decipher the following phrase: STANDS 0 _ 2 3 4 5 6 7 8 9

Upload: sakina

Post on 13-Feb-2016

27 views

Category:

Documents


0 download

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 29: Iterator and Iterable. List ADT. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Problem Of The Day

Problem Of The Day

Decipher the following phrase:

STANDS0 _ 2 3 4 5 6 7 8 9

Page 2: Problem Of The Day

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

LECTURE 29:ITERATOR AND ITERABLE

CSC 212 – Data Structures

Page 4: Problem Of The Day

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

ITERABLE

Why Should You Care?

Page 31: Problem Of The Day

Iterable Interface

So simple makes Iterator look complex

Page 32: Problem Of The Day

Iterable Interface

So simple makes Iterator look complex Get ready for this – it is really big!

Page 33: Problem Of The Day

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

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

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

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

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

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

Your Turn

Get into your groups and complete activity

Page 40: Problem Of The Day

About the Grading

Page 41: Problem Of The Day

For Next Lecture

Read GT 6.4 before Wednesday’s lecture What if we want indices & POSITIONs? Can we handle power to switch between the

two? What implementation issues do SEQUENCEs

cause?

Week #10 assignment available on Angel

Programming Assignment #2 due in 12 days