Problem of the day
This presentation is the property of its rightful owner.
Sponsored Links
1 / 41

Problem Of The Day PowerPoint PPT Presentation


  • 45 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Problem Of The Day

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Problem of the day

Problem Of The Day

  • Decipher the following phrase:

    STANDS

    0 _ 2 3 4 5 6 7 8 9


Problem of the day1

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

CSC 212 – Data Structures

Lecture 29:Iterator and Iterable


List adt

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

    • IndexListuses indices for absolution positioning

    • Can only use relative positions in NodeList


List adt1

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

    • IndexListuses indices for absolution positioning

    • Can only use relative positions in NodeList


List adt2

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 indicesfor absolution positioning

    • Can only use relative positions in NodeList


List adt3

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 indicesfor absolution positioning

    • Can only use relative positions in NodeList

      Must violate ADT to access List’s elements


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 indicesfor absolution positioning

    • Can only use relative positions in NodeList

      Must violate ADT to access List’s elements


Iterators

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 anyCollection


Iterators1

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 anyCollection


Using iterator

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

Iterator Interface

package java.util;

public interface Iterator<E> {booleanhasNext();Enext() throws NoSuchElementException;void remove() throws UnsupportedOperationException;

}


How iterators work

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator


How iterators work1

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work2

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work3

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work4

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work5

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work6

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work7

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


How iterators work8

How Iterators Work

  • Maintain a cursorshowing where they work

    • Value at cursor returned when next() called

  • Veryimplementation specific issues for cursor

    • To iterate over an IndexList, cursor must be index

    • Cursor is Position for PositionList’sIterator

List


Iterator for indexlist

Iteratorfor IndexList

public class IndexListIterator<E> {privateIndexList<E>theList;privateintcursor;public IndexListIterator(IndexList<E> list) {theList= list;cursor = 0;}

public booleanhasNext(){ returncursor!= theList.size();}// More goes here…


Limit of iterator

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


Position with benefits

Position with Benefits

  • cursoris next Positionin PositionList

  • Needing to know class specifics avoided

    • Rely on PositionList’s methods instead

head

tail

elements


Position with benefits1

Position with Benefits

  • cursoris next Positionin PositionList

  • Needing to know class specifics avoided

    • Rely on PositionList’s methods instead

head

tail

elements


Position with benefits2

Position with Benefits

  • cursoris next Positionin PositionList

  • Needing to know class specifics avoided

    • Rely on PositionList’s methods instead

head

tail

elements


Position with benefits3

Position with Benefits

  • cursoris next Positionin PositionList

  • Needing to know class specifics avoided

    • Rely on PositionList’s methods instead

head

tail

elements


Position with benefits4

Position with Benefits

  • cursoris next Positionin PositionList

  • Needing to know class specifics avoided

    • Rely on PositionList’s methods instead

head

tail

elements


Iterator for posiionlist

Iteratorfor PosiionList

privatePositionList<E>theList;privatePosition<E>cursor;public booleanhasNext(){ returncursor!= null;}public Enext() throwsNoSuchElementException{if (cursor== null) {throw new NoSuchElementException(); }EretVal=cursor.element(); if (cursor!=theList.last()) {cursor=theList.next(cursor);} else {cursor = null; } return retVal;}


Why should you care

Why Should You Care?

Iterable


Iterable interface

Iterable Interface

  • So simple makes Iterator look complex


Iterable interface1

Iterable Interface

  • So simple makes Iterator look complex

    • Get ready for this– it is really big!


Iterable interface2

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


Iterable interface3

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


Iterable interface4

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


Iterable interface5

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> {publicIterator<E> iterator();

    }


For each for the win

For-each for the Win

  • Iterablesupport built-into Java for free

    • As is any class in the java.lang package

  • For-each loops workwith any Iterable class

    • Uses Iteratorto loop over each element in class

  • Slightly different loop than normal for loopfor (Type variableName: IterableName)IndexList<Integer>idx = …;for (Integeri :idx) { … }


For each rocks the hizzy

For-Each Rocks The Hizzy

IntegerfindLargest(Iterable<Integer>able) {IntegerretVal=Integer.MIN_VALUE;for (Integerdatum:able) {if (datum>retVal) {retVal=datum; }}return retVal;

}

  • able could be List, HashMap, VertexSet…


Your turn

Your Turn

  • Get into your groups and complete activity


About the grading

About the Grading


For next lecture

For Next Lecture

  • Read GT 6.4 before Wednesday’s lecture

    • What if we wantindices & 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


  • Login