CS 2 in Java Peer Instruction Materials by
CS 2 in Java Peer Instruction Materials by Cynthia Lee is licensed under a Creative Commons Attribution-Non. Commercial 4. 0 International License. Based on a work at http: //peerinstruction 4 cs. org. Permissions beyond the scope of this license may be available at http: //peerinstruction 4 cs. org. CSE 12 – Basic Data Structures Cynthia Bailey Lee Some slides and figures adapted from Paul Kube’s CSE 12
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
Iterator interface
Reading quiz! Iterator. remove is not the only safe way to modify a collection during iteration. A. B. TRUE FALSE
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. B. TRUE FALSE
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. B. TRUE FALSE
Suppose we have a reference (current) to the node containing the item to be removed. head current (we want to remove this one) NULL 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.
iter: pred last (we want to remove this one) next head NULL 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
iter: pred list: head last (we want to remove this one) next NULL We initialize iterator as follows: this. pred = null; this. last = null; this. next = head; //predecessor of “last” node //last node returned //node to return next
iter: pred list: last next head data data next next X X X What are the iterator values after running the following code? Iterator iter = list. iterator(); iter. next(); A. B. C. D. pred is null, last is null, next points to A pred is null, last points to A, next points to B pred points to A, last points to B, next points to C Other/none/more
list: iter: pred last (we want to remove this one) next head NULL What are the iterator values after running the following code? Iterator iter = list. iterator(); iter. next(); A. pred points to A, last points to B, next points to C B. C. D. E. pred points to B, last points to C, next points to D pred points to C, last points to D, next points to E pred points to D, last points to E, next is null Other/none/more
list: head iter: pred last (we want to remove this one) next NULL What are the iterator values after running the following code? Iterator iter = list. iterator(); iter. next(); A. pred points to A, last points to B, next points to D iter. remove(); 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
list: head iter: pred last (we want to remove this one) next NULL What are the iterator values after running the following code? Iterator iter = list. iterator(); iter. next(); A. pred is null, last points to D, next points to E iter. remove(); B. pred points to B, last points to D, next points to E C. Other/none/more iter. next();
list: head iter: pred last (we want to remove this one) next NULL What are the iterator values after running the following code? Iterator iter = list. iterator(); iter. next(); A. pred points to B, last points to E, next points to E iter. next(); B. pred points to B, last points to E, next is null C. pred is null, last is null, next points to E iter. next(); D. Other/none/more iter. remove(); iter. next(); iter. remove();
Which code is more efficient? Iterator<String> i = c. iterator(); while ( i. has. Next() ) { String x = i. next(); if ( x. length() > 10 ) System. out. println(x); } } A. B. C. for( String x: c ) { if (x. length() > 10 ) { System. out. println(x); } } Right one is MORE efficient because it doesn’t involve iterators Right one is LESS efficient because it doesn’t involve iterators Other/none/moe
Which code is more efficient? Iterator<String> i = for( String x: c ) c. iterator(); { while ( i. has. Next() ) { if (x. length() > 10 ) { String x = i. next(); System. out. println(x); if ( x. length() > 10 ) } System. out. println(x); } Trick question! } The on the } right DOES use iterators! A. B. C. Right one is MORE efficient because it doesn’t involve iterators Right one is LESS efficient because it doesn’t involve iterators Other/none/moe
Java Nested/Inner Classes
Reading quiz! Suppose we have a linked list implementation that is defined as follows public class Singly. Linked. List<E> implements java. util. List<E> { private static class Node<T> { // nested class T data; Node<T> next; //etc… If we implement the Iterator } interface as an instance (i. e. private Node<E> head; nonstatic) inner class, its instance public Linked. List() { head = null; methods can access everything about //etc… its backing structure (the enclosing } Linked. List instance) //etc… A. TRUE B. FALSE
What this means for your project Node<E> List class does not need to access anything about the 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<E> { Static does not have access to outer class instance variables like head Design principle: Do not grant more access than necessary What about iterator?
Should L 12 Iter be a static nested class, or an (instance) inner class? A. B. private class L 12 Iter implements java. util. Iterator<E> { private static class L 12 Iter implements java. util. Iterator<E> { WHY?
Iterator does need access to list private variable head Remember this slide showing iterator constructor code:
What this means for your project L 12 Iter 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 L 12 Iter implements java. util. Iterator<E> {
What this means for your project Pitfall: Inner classes do not specify <E> parameter because they automatically match outer instance: private class L 12 Iter implements java. util. Iterator<E> { Static nested classes do specify <E> parameter because they do not have an outer instance private static class Node<E> {
Benchmarking code An alternative/supplement to big-O style analysis
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
IMPROVED procedure for benchmarking for problem size N = min, . . . max 1. initialize the data structure 2. for K runs: 1. 2. 3. 4. get the current (starting) time run the algorithm on problem size N get the current (finish) time timing = finish time – start time 3. Average timings for K runs for this N
Measurement accuracy 1. 2. 50, 1, 3, 34, 26, 10, 44, 36, 5, 37, 30 30, 22, 20, 27, 21, 29, 25, 22, 28, 26 Average is 25 in both cases Second set of measurements would inspire much more confidence than the first! Why? A. B. Standard deviation of first set of measurements is HIGHER than standard deviation of second set of measurements Standard deviation of first set of measurements is LOWER than standard deviation of second set of measurements
Explain these results. . . Timings for find. Max() on an array of ints and an array of Integers (times are in milliseconds) array of int array of Integer 800, 000 2. 314 4. 329 4, 000 11. 363 21. 739 8, 000 22. 727 42. 958 n From these measurements, what is the likely big-O time cost of find. Max() on array of int? A. B. C. f(n) = O(log 2 n) f(n) = O(n 2) D. E. f(n) = O(n) Other/none/more
Explain these results. . . Timings for find. Max() on an array of ints and an array of Integers (times are in milliseconds) array of int array of Integer 800, 000 2. 314 4. 329 4, 000 11. 363 21. 739 8, 000 22. 727 42. 958 n From these measurements, what is the likely big-O time cost of find. Max() on array of Integer? A. B. C. f(n) = O(log 2 n) f(n) = O(n 2) D. E. f(n) = O(n) Other/none/more
Explain these results. . . Timings for find. Max() on an array of ints and an array of Integers (times are in milliseconds) array of int array of Integer 800, 000 2. 314 4. 329 4, 000 11. 363 21. 739 8, 000 22. 727 42. 958 n Discussion: • Why would Integer take more time? • If Integer takes more time, why does it have the same Big-O cost as int?
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
- Slides: 31