Download presentation
Presentation is loading. Please wait.
Published byGeoffrey Jacobs Modified over 9 years ago
1
CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12 CS2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License. Based on a work at http://peerinstruction4cs.org. Permissions beyond the scope of this license may be available at http://peerinstruction4cs.org.Cynthia LeeCreative Commons Attribution-NonCommercial 4.0 International Licensehttp://peerinstruction4cs.org
2
Today’s Topics Algorithm analysis 1. Iterators 2. Java nested/inner classes 3. (If we have time) finish up a few thoughts on benchmarking of algorithms, from last time 2
3
Iterator interface
4
Reading quiz! Iterator.remove is not the only safe way to modify a collection during iteration. A. TRUE B. FALSE
5
Reading quiz! If we implement the Iterator interface as a public static inner class, then it can not be instantiated without an outer class instance A. TRUE B. FALSE
6
Reading quiz! If we implement the Iterator interface as a public static inner nested class, then it can not be instantiated without an outer class instance A. TRUE B. FALSE
7
Suppose we have a reference (current) to the node containing the item to be removed. What additional information do we need to successfully remove the node? A) Nothing additional. B) A reference to the node immediately prior to the deleted node. C) A reference to the node immediately after the node to be deleted. D) Both B and C. NULL head current (we want to remove this one)
8
Here we keep: A next pointer to what should be returned by the next call to next A last pointer to what should be deleted by the next call to remove (we remove the most recent element returned by a call to next ) A pred pointer to assist with implementing remove NULL head pred last (we want to remove this one) next iter:
9
We initialize iterator as follows: this.pred = null; //predecessor of “last” node this.last = null; //last node returned this.next = head; //node to return next NULL head list: pred last (we want to remove this one) next iter:
10
What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); head list: A.pred is null, last is null, next points to A B.pred is null, last points to A, next points to B C.pred points to A, last points to B, next points to C D.Other/none/more pred last next iter: next data X next data X next data X next data X next data X X
11
What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); NULL head list: A.pred points to A, last points to B, next points to C B.pred points to B, last points to C, next points to D C.pred points to C, last points to D, next points to E D.pred points to D, last points to E, next is null E.Other/none/more pred last (we want to remove this one) next iter:
12
What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); iter.remove(); NULL head list: pred last (we want to remove this one) next iter: A.pred points to A, last points to B, next points to D B.pred points to B, last points to D, next points to E C.pred points to B, last is null, next points to D D.pred is null, last is null, next points to D E.Other/none/more
13
What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); iter.remove(); iter.next(); NULL head list: pred last (we want to remove this one) next iter: A.pred is null, last points to D, next points to E B.pred points to B, last points to D, next points to E C.Other/none/more
14
What are the iterator values after running the following code? Iterator iter = list.iterator(); iter.next(); iter.remove(); iter.next(); iter.remove(); NULL head list: pred last (we want to remove this one) next iter: A.pred points to B, last points to E, next points to E B.pred points to B, last points to E, next is null C.pred is null, last is null, next points to E D.Other/none/more
15
Which code is more efficient? Iterator i = c.iterator(); while ( i.hasNext() ) { String x = i.next(); if ( x.length() > 10 ) System.out.println(x); } for( String x: c ) { if (x.length() > 10 ) { System.out.println(x); } A. Right one is MORE efficient because it doesn’t involve iterators B. Right one is LESS efficient because it doesn’t involve iterators C. Other/none/moe
16
Which code is more efficient? Iterator i = c.iterator(); while ( i.hasNext() ) { String x = i.next(); if ( x.length() > 10 ) System.out.println(x); } for( String x: c ) { if (x.length() > 10 ) { System.out.println(x); } A. Right one is MORE efficient because it doesn’t involve iterators B. Right one is LESS efficient because it doesn’t involve iterators C. Other/none/moe Trick question! The one on the right DOES use iterators!
17
Java Nested/Inner Classes
18
Reading quiz! Suppose we have a linked list implementation that is defined as follows public class SinglyLinkedList implements java.util.List { private static class Node { // nested class T data; Node next; //etc… } private Node head; public LinkedList() { head = null; //etc… } //etc… If we implement the Iterator interface as an instance (i.e. nonstatic) inner class, its instance methods can access everything about its backing structure (the enclosing LinkedList instance) A.TRUE B.FALSE
19
What this means for your project Node does not need to access anything about the List class An individual node has no need to know where the head of the list is Good idea to make this a static nested class: private static class Node { Static does not have access to outer class instance variables like head Design principle: Do not grant more access than necessary What about iterator?
20
Should L12Iter be a static nested class, or an (instance) inner class? A. private class L12Iter implements java.util.Iterator { B. private static class L12Iter implements java.util.Iterator { WHY?
21
Iterator does need access to list private variable head Remember this slide showing iterator constructor code:
22
What this means for your project L12Iter does need to access information about the outer List instance Iterator needs to know where the head of the list is Good idea to make this a non-static inner class: private class L12Iter implements java.util.Iterator {
23
What this means for your project Pitfall: Inner classes do not specify parameter because they automatically match outer instance: private class L12Iter implements java.util.Iterator { Static nested classes do specify parameter because they do not have an outer instance private static class Node {
24
Benchmarking code An alternative/supplement to big-O style analysis
25
Sources of inaccuracy in benchmarking Multiple processes running on system Open up Task Manager on your PC Or on Linux/UNIX or Mac, in command window run “ps –eaf | more” Even if you shut down all your major application programs, all kinds of utilities and other things still running Hardware interrupts Network activity Mouse/keyboard activity JVM garbage collection could start running unexpectedly
26
IMPROVED procedure for benchmarking for problem size N = min,...max 1. initialize the data structure 2. for K runs: 1. get the current (starting) time 2. run the algorithm on problem size N 3. get the current (finish) time 4. timing = finish time – start time 3. Average timings for K runs for this N
27
Measurement accuracy 1. 50, 1, 3, 34, 26, 10, 44, 36, 5, 37, 30 2. 30, 22, 20, 27, 21, 29, 25, 25, 22, 28, 26 Average is 25 in both cases Second set of measurements would inspire much more confidence than the first! Why? A. Standard deviation of first set of measurements is HIGHER than standard deviation of second set of measurements B. Standard deviation of first set of measurements is LOWER than standard deviation of second set of measurements
28
Explain these results... A. f(n) = O(log 2 n) B. f(n) = O(nlog 2 n) C. f(n) = O(n 2 ) D. f(n) = O(n) E. Other/none/more Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds) n array of int array of Integer 800,0002.3144.329 4,000,00011.36321.739 8,000,00022.72742.958 From these measurements, what is the likely big-O time cost of findMax() on array of int ?
29
Explain these results... A. f(n) = O(log 2 n) B. f(n) = O(nlog 2 n) C. f(n) = O(n 2 ) D. f(n) = O(n) E. Other/none/more Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds) n array of int array of Integer 800,0002.3144.329 4,000,00011.36321.739 8,000,00022.72742.958 From these measurements, what is the likely big-O time cost of findMax() on array of Integer ?
30
Explain these results... Timings for findMax() on an array of int s and an array of Integer s (times are in milliseconds) n array of int array of Integer 800,0002.3144.329 4,000,00011.36321.739 8,000,00022.72742.958 Discussion: Why would Integer take more time? If Integer takes more time, why does it have the same Big-O cost as int?
31
See Kube’s slides for HW tips! How to calculate standard deviation How to disable garbage collection so it doesn’t happen at unexpected times
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.