Download presentation
Presentation is loading. Please wait.
Published byAshlyn Blankenship Modified over 9 years ago
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 after an existing one Collection can remove any element it contains Loop over all elements without removing them List ADTs differ in how they provide access ArrayList’s indices give quick access to specific position Good at working at relative location with LinkedList
6
List ADT
10
Iterators Provides data access without knowing more As an ADT, methods independent of data storage Access elements 1-by-1 by calling next() method Like Comparator ; does not store data it uses Iterator class specific to instance holding data But means that code using Iterator independent Why is this important?
11
Iterators Provides data access without knowing more As an ADT, methods independent of data storage Access elements 1-by-1 by calling next() method Like Comparator ; does not store data it uses Iterator class specific to instance holding data But means that code using Iterator independent Why is this important? Breaks up code into separate class for each function Each part replaceable when better approach found
12
Perfect Example of Perfect Design
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
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
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 Moves forward item-by-item only, cannot jump around
17
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 Moves forward item-by-item only, cannot jump around
18
Limits to Iterator Iterator ’s methods are very, very limited Iterator instance has no data; limits possible uses While iterating, can be tempting to modify data Requires actual instance, I TERATOR does not store data If underlying instance changes, results undefined May work, may crash, or may do both; caveat emptor
19
Why Should You Care?
20
Iterable Interface So simple makes Iterator look complex Java’s prettiest feature relies on this interface
21
Iterable Interface So simple makes Iterator look complex Java’s prettiest feature relies on this interface
22
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(); }
23
Iterable Usage Occasionally use Iterator ; Iterable preferable Many ADT definitions include Iterable in definition Makes it possible to interchange ADT when processing Iterable support built-in to Java language Like Comparable, interface in java.lang package For-each loops through Iterable data directly Translates code to use Iterator (without extra code)
24
For-Each for the Win Slightly different loop than normal for loop for (type variableName : IterableVar) List idx; OrderedList profFirst; ArrayList whyNot; // Lots of code is here including assignments to our lists for (Integer i : idx ) { … } for (Costume cost : profFirst) { … } for (Scanner stupid : whyNot) { … }
25
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 A RRAY L IST & L INKED L IST are examples; many will follow
26
What Type Should Cursor Be?
28
Implementing Iterator Very implementation specific issues for cursor To iterate over an A RRAY L IST, cursor is index Cursor is node for L INKED 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
29
What Type Should Cursor Be?
30
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
31
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
32
Implementing Iterator (Start) public class ALIterator { private ArrayList theList; private int cursor; public ALIterator(ArrayList list) { theList = list; cursor = 0; } } public class LLIterator { private LinkedList theList; private DNode cursor; public LLIterator(LinkedList list) { theList = list; cursor = } }
33
Implementing Iterator (Start) public class ALIterator { private ArrayList theList; private int cursor; public ALIterator(ArrayList list) { theList = list; cursor = 0; } } public class LLIterator { private LinkedList theList; private DNode cursor; public LLIterator(LinkedList list) { theList = list; cursor = } }
34
Implementing the Iterator Iterator class needs access to List’s internal fields Implementationless ADT concept must be violated Code specific to implementation and not reusable All CSC212 design concepts get thrown out window
35
Implementing the Iterator Iterator class needs access to List’s internal fields Implementationless ADT concept must be violated Code specific to implementation and not reusable All CSC212 design concepts get thrown out window
36
Implementing for ArrayList L IST
37
Implementing for ArrayList L IST ALIterator cursor 0 theList
38
Implementing for ArrayList L IST ALIterator cursor 1 theList
39
Implementing for ArrayList L IST ALIterator cursor 2 theList
40
L IST Implementing for ArrayList ALIterator cursor 3 theList
41
L IST Implementing for ArrayList ALIterator
42
L IST Implementing for ArrayList ALIterator cursor 4 theList
43
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
44
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
45
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
46
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
47
Implementing for LinkedList cursor is next node in LinkedList Need to know class specifics to use node types No methods in LinkedList provide required info head tail LLIterator cursor theList
48
Comparing Implementations ArrayList Iterator LinkedList Iterator Check if cursor valid: cursor != theList.size () Get element at cursor : theList.get ( cursor ) or array[cursor] Advancing cursor : cursor += 1 Check if cursor valid: cursor != null Get element at cursor : cursor.getElement () Advancing cursor : cursor = cursor. getNext ()
49
Your Turn Get into your groups and complete activity
50
For Next Lecture Read 8.1 – 8.4 before Friday’s lecture What is recursion & how does it work? What happens to locals if method called again? Why did we spend so much time on tracing methods? Week #11 assignment available on Angel No class on Monday No class on Monday; I will be out of town As a result, I am delaying quiz until Wednesday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.