Course Announcements. John Jannotti (cs32) Scope, Collections & Generics Feb 8, / 1

Similar documents
Announcements. John Jannotti (cs32) Design Patterns Feb 13, / 1

1 Trees. Listing 1: Node with two child reference. public class ptwochildnode { protected Object data ; protected ptwochildnode l e f t, r i g h t ;

MURA Office hours Thurs at 12-1pm in CIT 546 Contact for more info.

Math 3361-Modern Algebra Lecture 08 9/26/ Cardinality

Course Announcements. Bacon is due next Monday. Next lab is about drawing UIs. Today s lecture will help thinking about your DB interface.

Descriptive Statistics (And a little bit on rounding and significant digits)

ITI Introduction to Computing II

Computer Science Introductory Course MSc - Introduction to Java

Lecture 5: Sep. 23 &25

Voortgezet Programmeren

Tou has been released!

ITI Introduction to Computing II

Announcements. CSE332: Data Abstractions Lecture 2: Math Review; Algorithm Analysis. Today. Mathematical induction. Dan Grossman Spring 2010

Outline Inverse of a Relation Properties of Relations. Relations. Alice E. Fischer. April, 2018

CS173 Lecture B, November 3, 2015

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, Name:

CS-140 Fall 2018 Test 2 Version Practice Nov. 12, 2018

UI Implementation. John Jannotti. Mar 21, /course/cs0320/www/docs/lectures/ John Jannotti (cs32) UI Implementation Mar 21, / 26

Introduction to Computing II (ITI1121) FINAL EXAMINATION

CSE373: Data Structures and Algorithms Lecture 2: Math Review; Algorithm Analysis. Hunter Zahn Summer 2016

Data Structures and Algorithms Winter Semester

One-to-one functions and onto functions

Maps performance tips On server: Maintain DB connections, prepared statements (per thread/request!)

FACULTY OF SCIENCE ACADEMY OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING OBJECT ORIENTED PROGRAMMING DATE 07/2014 SESSION 8:00-10:00

CS-140 Fall 2017 Test 1 Version Practice Practice for Nov. 20, Name:

CS 4110 Programming Languages & Logics. Lecture 25 Records and Subtyping

Q: How can quantum computers break ecryption?

CS Java. Introduction to Java. Andy Mroczkowski Department of Computer Science Drexel University

Sharing Objects. Pieter van den Hombergh. Fontys Hogeschool voor Techniek en Logistiek. February 15, 2017

Lecture 14: Nov. 11 & 13

Sequential Circuit Analysis

CS221 Practice Midterm #2 Solutions

CS 3110: Proof Strategy and Examples. 1 Propositional Logic Proof Strategy. 2 A Proof Walkthrough

CS1800: Strong Induction. Professor Kevin Gold

Latches. October 13, 2003 Latches 1

Numerical Methods Lecture 2 Simultaneous Equations

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Intro to Learning Theory Date: 12/8/16

Algebra. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

CS Exam 1 Study Guide and Practice Exam

HW8. Due: November 1, 2018

Adam Blank Spring 2017 CSE 311. Foundations of Computing I. * All slides are a combined effort between previous instructors of the course

SFWR ENG/COMP SCI 2S03 Principles of Programming

Natural deduction for truth-functional logic

ASTRO 114 Lecture Okay. We re now gonna continue discussing and conclude discussing the entire

Lecture 3. Big-O notation, more recurrences!!

Lecture 8 HASHING!!!!!

TheFourierTransformAndItsApplications-Lecture28

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

Computational complexity and some Graph Theory

AP Physics Newton s Laws Wrap-up

Projectile Motion. v a = -9.8 m/s 2. Good practice problems in book: 3.23, 3.25, 3.27, 3.29, 3.31, 3.33, 3.43, 3.47, 3.51, 3.53, 3.

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Catie Baker Spring 2015

CS173 Strong Induction and Functions. Tandy Warnow

Discrete Structures Proofwriting Checklist

Student Activity: Finding Factors and Prime Factors

Numerical Methods Lecture 2 Simultaneous Equations

Math (P)Review Part I:

Lesson 21 Not So Dramatic Quadratics

KIRCHHOFF S LAWS. Learn how to analyze more complicated circuits with more than one voltage source and numerous resistors.

Computer Systems on the CERN LHC: How do we measure how well they re doing?

Chapter Finding parse trees

Lecture 1: Asymptotic Complexity. 1 These slides include material originally prepared by Dr.Ron Cytron, Dr. Jeremy Buhler, and Dr. Steve Cole.

An Intuitive Introduction to Motivic Homotopy Theory Vladimir Voevodsky

PHYSICS 15a, Fall 2006 SPEED OF SOUND LAB Due: Tuesday, November 14

Solving with Absolute Value

CS173 Lecture B, September 10, 2015

Grades 7 & 8, Math Circles 10/11/12 October, Series & Polygonal Numbers

Big Bang, Black Holes, No Math

CSE 373: Data Structures and Algorithms Pep Talk; Algorithm Analysis. Riley Porter Winter 2017

ASTRO 114 Lecture gonna be talking about individual galaxies, but we re gonna be talking about the whole

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Astronomical Distances. Astronomical Distances 1/30

(Refer Slide Time: 0:21)

Deterministic Finite Automaton (DFA)

Lecture 2. More Algorithm Analysis, Math and MCSS By: Sarah Buchanan

3) x -7 4) 3 < x. When multiplying or dividing by a NEGATIVE number, we SWITCH the inequality sign!

You separate binary numbers into columns in a similar fashion. 2 5 = 32

Chapter Finding parse trees

CS1083 Week 4 : Polymorphism

Unit #2: Linear and Exponential Functions Lesson #13: Linear & Exponential Regression, Correlation, & Causation. Day #1

1. Write a program to calculate distance traveled by light

Chemical Applications of Symmetry and Group Theory Prof. Manabendra Chandra Department of Chemistry Indian Institute of Technology, Kanpur.

After that, we will introduce more ideas from Chapter 5: Number Theory. Your quiz in recitation tomorrow will involve writing proofs like those.

Chemical Applications of Symmetry and Group Theory Prof. Manabendra Chandra Department of Chemistry Indian Institute of Technology, Kanpur

Basic Java OOP 10/12/2015. Department of Computer Science & Information Engineering. National Taiwan University

CS1210 Lecture 23 March 8, 2019

Sequential Logic (3.1 and is a long difficult section you really should read!)

C if U can. Algebra. Name

It ain t no good if it ain t snappy enough. (Efficient Computations) COS 116, Spring 2011 Sanjeev Arora

Water tank. Fortunately there are a couple of objectors. Why is it straight? Shouldn t it be a curve?

AN ALGEBRA PRIMER WITH A VIEW TOWARD CURVES OVER FINITE FIELDS

Binary addition example worked out

Calculus II. Calculus II tends to be a very difficult course for many students. There are many reasons for this.

Lecture 4: Constructing the Integers, Rationals and Reals

Minilecture 2: Sorting Algorithms

Introduction to Computing II (ITI 1121) FINAL EXAMINATION

Chapter 1 Review of Equations and Inequalities

Newton s Law of Motion

Transcription of Science Time video Spring and Flowers

CS 170 Algorithms Spring 2009 David Wagner Final

Transcription:

Course Announcements Stars is due tomorrow. Stars grades should be out by next Monday. Javascript lab out today. How you make interactive webby GUIs. Today we re going to cover a bit of a hodge podge. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 1 / 1

Scope, Collections & Generics John Jannotti /course/cs0320/www/lectures/ Feb 8, 2018 John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 2 / 1

A little food for thought on Stars Should my program do this obviously correct thing? Yes. What do you do if two stars have the same coordinate? What do you do if two stars have the same name? Do you report more that one error in a CSV? Do you report where an error in CSV occurred? Can you easily you add new commands? Is your command processing reusable? How did you write tests for your command processing? Can you add() to your KdTree? remove()? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 3 / 1

Dynamic / Static Dispatch 1 c l a s s Person { 2 S t r i n g c o n f e s s ( Object msg ) { r e t u r n msg. t o S t r i n g ( ) ; 3 } 4 c l a s s Spy e x t e n d s Person { 5 S t r i n g c o n f e s s ( S t r i n g msg ) { r e t u r n Never! ; } 6 } 7 Person p = new Person ( ) ; 8 p. c o n f e s s ( The dock, at 11am ) ; 9 Spy s1 = new Spy ( ) ; 0 s1. c o n f e s s ( The a i r p o r t, at 12pm ) ; 1 Person p2 = s1 ; 2 p2. c o n f e s s ( The depot, at 6pm ) ; 3 Spy s3 = new Spy ( ) ; 4 s3. c o n f e s s ( new S t r i n g B u i l d e r ( The stadium, at 3pm ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 4 / 1

equals / hashcode 1 c l a s s L a b e l { 2 p r i v a t e f i n a l S t r i n g t e x t ; 3 p u b l i c L a b e l ( S t r i n g t ) { t e x t = t ; } 4 } 5 Set<Label > seen = new HashSet <>(); 6 seen. add ( new L a b e l ( John ) ) ; 7 seen. c o n t a i n s ( new L a b e l ( John ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 5 / 1

equals / hashcode 1 c l a s s L a b e l { 2 p r i v a t e f i n a l S t r i n g t e x t ; 3 p u b l i c L a b e l ( S t r i n g t ) { t e x t = t ; } 4 p u b l i c boolean e q u a l s ( Object o ) { 5 / assume c o r r e c t, b a s i c a l l y comparing t e x t / 6 } 7 } 8 Set<Label > seen = new HashSet <>(); 9 seen. add ( new L a b e l ( John ) ) ; 0 seen. c o n t a i n s ( new L a b e l ( John ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 6 / 1

Snapshot Diagrams A standard way to draw the execution state of a program. Primitives are draw bare. Objects are shown as circles, labeled by type. Details as needed. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 7 / 1

Mutability Modifying an object is changing what s in the circle. We show immutable objects with a double border. Changing them is really reassignment. We use a double arrow to show a reference that can t be changed. ( final ). John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 8 / 1

Local and Global Variables 1 c l a s s Payment { 2 p u b l i c d o u b l e v a l u e ; 3 p u b l i c s t a t i c d o u b l e taxrate = 0. 0 5 ; 4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] a r g s ) { 5 Payment p = new Payment ( ) ; 6 p. v a l u e = 1 0 0 ; 7 taxrate = 0. 0 5 ; 8 System. out. p r i n t l n ( p. v a l u e (1 + taxrate ) ) ; 9 } 0 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 9 / 1

Now we can talk about scopes The basics: static methods can see their own local variables, and static fields of their class. instance methods can see their locals, the static fields and the instance fields (through this). In both, variables of inner blocks can t be seen outside. Strive to minimize scope. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 10 / 1

Now we can talk about scopes The basics: static methods can see their own local variables, and static fields of their class. instance methods can see their locals, the static fields and the instance fields (through this). In both, variables of inner blocks can t be seen outside. Strive to minimize scope. Now the tricky stuff. Inner class, both static and normal. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 10 / 1

Generics Many abstractions are generic across types. Holders: Optional, ThreadLocal Factory methods: ImmutableList.of(), DB.getObject(Company.class, id) Collections: List<String>, Map<Label, Person> Before Generics, these abstractions took/returned Object. Return values were cast to the (statically known) return type. The compiler couldn t help with mistakes, refactoring. ClassCastException was a common error. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 11 / 1

Generic comparison 1 / 2 @param words must c o n t a i n o n l y S t r i n g 3 / 4 v o i d swear ( C o l l e c t i o n words ) { 5 f o r ( Object o : words ) { 6 S t r i n g word = ( S t r i n g ) o ; 7 i f ( word. l e n g t h ( ) == 4) 8 System. out. p r i n t l n ( word ) ; 9 } 0 } 1 2 v o i d swear ( C o l l e c t i o n <S t r i n g > words ) { 3 f o r ( S t r i n g word : words ) 4 i f ( word. l e n g t h ( ) == 4) 5 System. out. p r i n t l n ( word ) ; 6 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 12 / 1

Writing generic abstractions You ve probably all used generic abstractions. Have you written any? What? Simple things are pretty simple. Complex things can be very, very complex. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 13 / 1

Pair.java 1 p u b l i c c l a s s Pair <A, B> { 2 p r i v a t e f i n a l A l e f t ; 3 p r i v a t e f i n a l B r i g h t ; 4 5 p u b l i c P a i r (A a, B b ) { 6 l e f t = a ; 7 r i g h t = b ; 8 } 9 0 @Override 1 p u b l i c S t r i n g t o S t r i n g ( ) { 2 r e t u r n ( +l e f t+. +r i g h t+ ) ; 3 } 4 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 14 / 1

Type constraints Why isn t this legal? 1 L i s t <Pair <I n t e g e r, S t r i n g >> l s t = 2 I m m u t a b l e L i s t. o f ( new Pair <>(4, john ), 3 new Pair <>(2, mary ), 4 new Pair <>(7, pat ) ) ; 5 C o l l e c t i o n s. s o r t ( a ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 15 / 1

Type constraints Why isn t this legal? 1 L i s t <Pair <I n t e g e r, S t r i n g >> l s t = 2 I m m u t a b l e L i s t. o f ( new Pair <>(4, john ), 3 new Pair <>(2, mary ), 4 new Pair <>(7, pat ) ) ; 5 C o l l e c t i o n s. s o r t ( a ) ; Because Pair<Integer,String> does not implement Comparable<Pair<Integer,String>> John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 15 / 1

Can we implement Comparable? 1 p u b l i c c l a s s Pair <A, B> { 2 p u b l i c i n t compareto ( Pair <A, B> o t h e r ) { 3... a. compareto ( o t h e r. a )... 4 } 5 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 16 / 1

Can we implement Comparable? 1 p u b l i c c l a s s Pair <A, B> { 2 p u b l i c i n t compareto ( Pair <A, B> o t h e r ) { 3... a. compareto ( o t h e r. a )... 4 } 5 } No. The types A & B might not, themselves, be Comparable<A> and Comparable<B> John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 16 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? Can you pass in a List<Circle>? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? Can you pass in a List<Circle>? draw(list<?>);? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Manipulating generic objects Suppose your printall() handles any Collection. Iterates over the members and calls tostring() What s the signature of printall()? void printall(collection c);? void printall(collection<object> c);? void printall(collection<?> c); What if you want to call draw() on a list of any Shapes? draw(list<shape> shapes);? Can you pass in a List<Circle>? draw(list<?>);? draw(list<? extends Shape>); John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 17 / 1

Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1

Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1

Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. In 3D space? KD? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1

Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. In 3D space? KD? You want a constraint like draw() has. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1

Your KDTree You should have a generic KDTree. You ll thank me later. What makes KDTree<T> harder than List<T>? KDTrees are not completely generic. Items must have positions. In 3D space? KD? You want a constraint like draw() has. If you have internal classes, they probably need the same constraint. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 18 / 1

KDTree as Collection Consider implementing Collection. Someone else has thought about completeness. You will interoperate with more code. But (it seems like) you need to implement 15 methods! John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 19 / 1

KDTree as Collection Consider implementing Collection. Someone else has thought about completeness. You will interoperate with more code. But (it seems like) you need to implement 15 methods! You can extend AbstractCollection to get a leg up. You ll just need size and iterator. You ought to provide a constructor from Collection. How about add? That iterator is a bit challenging. Allows for (Star s : tree) { } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 19 / 1

Enhanced For Loop 1 // Old s t y l e 2 s t a t i c v o i d swear ( C o l l e c t i o n <S t r i n g > c ) { 3 f o r ( I t e r a t o r <S t r i n g > i = c. i t e r a t o r ( ) ; i. hasnext ( ) ; ) 4 S t r i n g word = i. next ( ) ; 5 i f ( word. l e n g t h ( ) == 4) 6 System. out. p r i n t l n ( word ) ; 7 } 8 } 9 0 // New s t y l e 1 s t a t i c v o i d swear ( C o l l e c t i o n <S t r i n g > c ) { 2 f o r ( S t r i n g word : c ) 3 i f ( word. l e n g t h ( ) == 4) 4 System. out. p r i n t l n ( word ) ; 5 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 20 / 1

How does the new for loop work? It s just syntactic sugar Essentially compiles to the Iterator code. What s required? c.iterator() A new interface: Iterable Key point: c is Iterable, not an Iterator. An Iterator abstracts one iteration, Iterable abstracts the ability to be iterated over. When will you still use the iterator code pattern? When you need access to Iterator.remove() John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 21 / 1

Why should you bother? Java made many things Iterable Arrays Collections or all kinds So why should you bother makeing your types Iterable? You could just return one of the iterable types. And clients would still get to use the nice for-loops. Remember to copy or wrap modifiable objects. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 22 / 1

Why should you bother? Java made many things Iterable Arrays Collections or all kinds So why should you bother makeing your types Iterable? You could just return one of the iterable types. And clients would still get to use the nice for-loops. Remember to copy or wrap modifiable objects. But consider allowing iteration over a binary tree. Or your KDTree. Why can t we lean on the existing collections? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 22 / 1

Why should you bother? Java made many things Iterable Arrays Collections or all kinds So why should you bother makeing your types Iterable? You could just return one of the iterable types. And clients would still get to use the nice for-loops. Remember to copy or wrap modifiable objects. But consider allowing iteration over a binary tree. Or your KDTree. Why can t we lean on the existing collections? You don t want to have to copy your (possibly big) tree into another collection for each call. John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 22 / 1

Making BitSet Iterable Java library provides BitSet A compact representation for a set of (small) integers. Uses an array of integers internally. Each 32-bit integer records the presence/absence of 32 integers. But it isn t Iterable How would we make an Iterable BitSet? What primitives do you need BitSet to have? Anything else you would like? How do you structure the classes? John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 23 / 1

Write a test first 1 @Test p u b l i c v o i d t e s t C o n s t r u c t i o n ( ) { 2 I n t S e t s e t = new I n t S e t ( 2 1, 1 0, 1 3 ) ; 3 Set<I n t e g e r > hs = S e t s. newhashset ( 1 3, 1 0, 2 1 ) ; 4 a s s e r t E q u a l s ( hs. s i z e ( ), s e t. c a r d i n a l i t y ( ) ) ; 5 a s s e r t T r u e ( s e t. get ( 2 1 ) ) ; 6 a s s e r t T r u e ( s e t. get ( 1 0 ) ) ; 7 a s s e r t T r u e ( s e t. get ( 1 3 ) ) ; 8 a s s e r t F a l s e ( s e t. get ( 0 ) ) ; 9 a s s e r t F a l s e ( s e t. get ( 1 ) ) ; 0 a s s e r t F a l s e ( s e t. get ( 1 1 ) ) ; 1 2 f o r ( i n t i : s e t ) { 3 a s s e r t T r u e ( hs. c o n t a i n s ( i ) ) ; 4 hs. remove ( i ) ; 5 } 6 a s s e r t T r u e ( hs. isempty ( ) ) ; John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 24 / 1

IntSet, an Iterable BitSet 1 p u b l i c c l a s s I n t S e t e x t e n d s B i t S e t 2 implements I t e r a b l e <I n t e g e r > { 3 p u b l i c I n t S e t ( I n t e g e r... i n t s ) { 4 s u p e r ( ) ; 5 f o r ( i n t i : i n t s ) 6 s e t ( i ) ; 7 } 8 p u b l i c I t e r a t o r <I n t e g e r > i t e r a t o r ( ) { 9 r e t u r n new I t r ( ) ; 0 } 1 p r i v a t e c l a s s I t r implements I t e r a t o r <I n t e g e r > { 2... 3 } 4 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 25 / 1

IntSet$Itr 1 p r i v a t e c l a s s I t r implements I t e r a t o r <I n t e g e r > { 2 i n t c u r s o r = 1; 3 p u b l i c boolean hasnext ( ) { 4 r e t u r n n e x t S e t B i t ( c u r s o r +1) >= 0 ; 5 } 6 p u b l i c I n t e g e r next ( ) { 7 i n t b i t = n e x t S e t B i t ( c u r s o r +1); 8 i f ( b i t < 0) 9 throw new NoSuchElementException ( ) ; 0 c u r s o r = b i t ; 1 r e t u r n c u r s o r ; 2 } 3 p u b l i c v o i d remove ( ) { c l e a r ( c u r s o r ) ; } 4 } John Jannotti (cs32) Scope, Collections & Generics Feb 8, 2018 26 / 1