Presentation is loading. Please wait.

Presentation is loading. Please wait.

Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

Similar presentations


Presentation on theme: "Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)"— Presentation transcript:

1 Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)

2 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)

3 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

4 CSC 212 – Data Structures

5 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

6 List ADT

7

8

9 Oops…

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

12 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

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

14 Iterator Needs a Cursor

15 Implementing Iterator (Start) public class ILIterator { private IndexList theList; private int cursor; public ILIterator(IndexList list) { theList = list; cursor = 0; } } public class PLIterator { private PositionList theList; private Position cursor; public PLIterator(PositionList list) { theList = list; cursor = list.first(); // TODO: handle empty list correctly } }

16 Limits to Iterator  I TERATOR ’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  Only moves forward item-by-item, cannot jump around  While iterating, can be tempting to modify data  Easy to do, as object being iterated over is not I TERATOR  But result undefined, if data changes, I TERATOR can crash

17 Implementing Iterator  Very implementation specific issues for cursor  To iterate over an I NDEX L IST, cursor is index  Cursor is P OSITION for P OSITION L IST ’s I TERATOR  Iterator’s methods always use similar algorithm  General outline identical, since interface defines task  But implementation changes since cursor use differs

18 Implementing Iterator

19

20 Same Algorithm, But... Algorithm next() if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retVal else throw new NoSuchElementException endif end

21 Same Algorithm, But... Algorithm next() if hasNext() then E retVal = value at cursor’s location Advance cursor to refer to next location return retVal else throw new NoSuchElementException endif end

22 Implementing for IndexList L IST

23 Implementing for IndexList ILIterator cursor  0 theList L IST

24 Implementing for IndexList L IST ILIterator cursor  0 theList

25 Implementing for IndexList L IST ILIterator cursor  1 theList

26 Implementing for IndexList L IST ILIterator cursor  2 theList

27 Implementing for IndexList L IST ILIterator cursor  3 theList

28 Implementing for IndexList L IST ILIterator cursor  ? theList

29 Implementing for IndexList L IST ILIterator cursor  5 theList

30 Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

31 Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

32 Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

33 Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator cursor theList

34 Implementing for PositionList  cursor is next Position in PositionList  Needing to know class specifics avoided  Rely on P OSITION L IST ’s methods instead head tail PLIterator  cursor theList

35 Comparing Implementations IndexList Iterator PositionList Iterator  Check if cursor valid: cursor != theList.size ()  Get element at cursor : theList.get ( cursor )  Advancing cursor : cursor += 1  Check if cursor valid: cursor != null  Get element at cursor : cursor.element ()  Advancing cursor : cursor = theList.next ( cursor )

36 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

37 Why Should You Care?

38 Iterable Interface  So simple makes Iterator look complex  Java’s prettiest feature relies on this interface

39 Iterable Interface  So simple makes Iterator look complex  Java’s prettiest feature relies on this interface

40 Iterable Interface  So simple makes Iterator look complex  Java’s prettiest feature relies on this interface package java.lang; public interface Iterable { public Iterator iterator(); }

41 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) { … }

42 For-Each Rocks The Hizzy Integer findSum(Iterable able) { Integer retVal = 0; for (Integer datum : able) { retVal += datum; } return retVal; }  able could be (almost) ANY C OLLECTION class  I NDEX L IST & P OSITION L IST first examples; many follow

43 Your Turn  Get into your groups and complete activity

44 For Next Lecture  Read GT 6.4 before Monday’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 #11 assignment available on Angel  Programming Assignment #2  Programming Assignment #2 due in 7 days


Download ppt "Problem of the Day  Simplify this equation: (x - a) * (x - b) * (x - c) * … * (x - z)"

Similar presentations


Ads by Google