Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Problem Of The Day  Decipher the following phrase: STANDS 0 _ 2 3 4 5 6 7 8 9."— Presentation transcript:

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

2 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

3 CSC 212 – Data Structures

4 List ADT  Collection which we can access all elements  Add element before an existing one  Return the Collection’s 3 rd element  Loop over all elements without removing them  L IST ADTs differ in how they provide access  I NDEX L IST uses indices for absolution positioning  Can only use relative positions in N ODE L IST

5 List ADT

6

7

8 Oops…

9 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 any  Makes it easy to write code using any C OLLECTION

10 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 any  Makes it easy to write code using any C OLLECTION

11  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

12 package java.util; public interface Iterator { boolean hasNext(); E next() throws NoSuchElementException; void remove() throws UnsupportedOperationException; } Iterator Interface

13  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work

14  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST

15  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work Iterator Cursor  0 L IST

16  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  0

17  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  1

18  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  2

19  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  3

20  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  ???

21  Maintain a cursor showing where they work  Value at cursor returned when next() called  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor must be index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR How Iterators Work L IST Iterator Cursor  5

22 Iterator for I NDEX L IST public class IndexListIterator { private IndexList theList; private int cursor; public IndexListIterator(IndexList list) { theList = list; cursor = 0; } public boolean hasNext() { return cursor != theList.size(); } // More goes here…

23 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

24 P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

25 P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

26 P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

27 P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

28 P OSITION with Benefits  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail elements Iterator Cursor

29 Iterator for P OSIION L IST private PositionList theList; private Position 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; }

30 Why Should You Care?

31 Iterable Interface  So simple makes Iterator look complex

32 Iterable Interface  So simple makes Iterator look complex  Get ready for this – it is really big!

33 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

34 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

35 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

36 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 { public Iterator iterator(); }

37 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 loop for (Type variableName : IterableName) IndexList idx = …; for (Integer i : idx) { … }

38 Integer findLargest(Iterable able) { Integer retVal = Integer.MIN_VALUE; for (Integer datum : able) { if (datum > retVal) { retVal = datum; } } return retVal; }  able could be L IST, H ASH M AP, V ERTEX S ET … For-Each Rocks The Hizzy

39 Your Turn  Get into your groups and complete activity

40 About the Grading

41 For Next Lecture  Read GT 6.4 before Wednesday’s lecture  What if we want indices & P OSITION s?  Can we handle power to switch between the two?  What implementation issues do S EQUENCE s cause?  Week #10 assignment available on Angel  Programming Assignment #2  Programming Assignment #2 due in 12 days


Download ppt "Problem Of The Day  Decipher the following phrase: STANDS 0 _ 2 3 4 5 6 7 8 9."

Similar presentations


Ads by Google