Programming Language Technologies and Paradigms | CMSC 433, Study notes of Programming Languages

Material Type: Notes; Professor: Sussman; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-mla-3
koofers-user-mla-3 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Inner Classes & Iterators
Mar. 6, 2007
2
Inner Classes
Classes can be nested inside other classes
These are called inner classes
Within a class that contains an inner class, you can
use the inner class just like any other class
3
Example: The Queue Class
class Queue<Element> {
class Entry { // Java inner class
Element elt; Entry next;
Entry(Element i) { elt = i; next = null; }
}
Entry theQueue;
void enqueue(Element e) {
if (theQueue == null) theQueue = new Entry(e);
else {
Entry last = theQueue;
while (last.next != null) last = last.next;
last.next = new Entry(e);
}
}
...
4
Example: The Queue Class (cont’d)
class Queue<Element> {
...
Element dequeue() throws EmptyQueueException {
if (theQueue == null)
throw new EmptyQueueException();
Element e = theQueue.elt;
theQueue = theQueue.next;
return e;
}
}
pf3
pf4
pf5

Partial preview of the text

Download Programming Language Technologies and Paradigms | CMSC 433 and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Spring 2007

Inner Classes & Iterators

Mar. 6, 2007

Inner Classes

Classes can be nested inside other classes

– These are called

inner classes

use the inner class just like any other classWithin a class that contains an inner class, you can

3

Example: The Queue Class

class

Queue {

class Entry

Java

inner class

Element elt; Entry

next;

Entry(Element

i) {

elt

= i;

next =

null; }

void enqueue(ElementEntry theQueue;}

e) {

if (theQueue ==

null) theQueue

= new Entry(e);

else

Entry last =

theQueue;

while (last.next

!= null)

last =

last.next;

last.next =

new Entry(e);

Example: The Queue Class (cont’d)

class

Queue {

Element dequeue() throws...

EmptyQueueException

if (theQueue

== null)

throw

new EmptyQueueException();

Element

e = theQueue.elt;

theQueue

= theQueue.next;

return e;

5

Referring to Outer Class

class Queue

class Entry {int numEntries;...

Entry(Element i) { elt = i; nextElement elt; Entry next;

= null;

numEntries++;

the outer “object” whose method created itEach inner “object” has an implicit reference to

– Can refer to fields directly, or use outer class name.

Anonymous Inner Classes

(new

Thread() {

public void run() {

} System.exit(1);System.out.println(”...");Thread.sleep(10006020);try {

catch

(Exception e)

}).start();

method on itCreate anonymous subclass of thread, and invoke

7

Other Features of Inner Classes

to refer to type of inner classOutside of the outer class, use outer.inner notation

– E.g., Queue.Entry

An inner class marked

static

does not have a

reference to outer class

– Must also use outer.inner notation to refer to inner class– Can’t refer to instance variables of outer class

Compiling Inner Classes

The JVM doesn’t know about inner classes

– Anonymous inner class of outer class A produces– Inner class Foo of outer class A produces A$Foo.class– Compiled away, similar to generics

A$1.class

Why are inner classes useful?

13

Using Iterators (contd…)

private static

void

useWhileLoop(

Collection

aFlavours

Iterator flavoursIter

= aFlavours.iterator();

while

( flavoursIter.hasNext()

System.out.println( flavoursIter.next() );

Note

that

this

for-loop

does

not use an

integer

index.

private*/

static void useForLoop(

Collection

aFlavours

for (

Iterator

flavoursIter =

aFlavours.iterator();

flavoursIter.hasNext();

System.out.println( flavoursIter.next() );

Iterators and Queues

Recall queue example from beginning of lecture

We’ll explore options for adding iterators

15

next() Shouldn’t Mutate Aggregate

class

Queue {

class QueueIterator implements...

Iterator {

Entry

rest;

QueueIterator(Entry

q)

{ rest

= q;

boolean hasNext() {

return

rest !=

null; }

Element next()

throws NoSuchElementException

if

(rest

null)

throw new NoSuchElementException();

Element

e =

rest.elt;

rest =

rest.next;

queue

data

intact

return

e;

Evil Mutating Clients

But a client could mutate the data structure …

HashMap h

= ...;

... //entrySet() Returns a

collection view of

the mappings

//contained in

this map.

Iterator i

= h.entrySet().iterator();

System.out.println(i.next());System.out.println(i.next()); //// put(Object key, Object value)

Associates the

specified value

//with the specified key in this map.

h.put(“Foo”, “Bar”); //

hash table resize!

System.out.println(i.next());

//

prints ???

17

Defensive (Proactive) Copying

Solution 1: Iterator copies data structure

class

QueueIterator

implements

Iterator

Entry

rest;

QueueIterator(Queue

q)

{

copy

q.theQueue to

rest

Pro: Works even if queue is mutated

Con: Expensive to construct iterator

Timestamps

Solution 2: Track Mutations

class

Queue {

int...

modCount

void enqueue(Element

e) {

modCount++;

Element

dequeue()

{ ... modCount++;

19

Timestamps (cont’d)

class QueueIterator implements ...

Iterator {

int expectedModCount =

modCount; // set

at

iterator

construction time

Element next()

if

(expectedModCount

!= modCount)

throw

new

ConcurrentModificationException();

// does hasNext() need}

to be

modified?

Pro: Iteration construction cheap

Con: Doesn’t allow any mutation

What if Mutation is Allowed?

Allowed mutation must be part of iterator spec

public

void

remove()

throws IllegalStateException;

per call to next.the iterator (optional operation). This method can be called only onceRemoves from the underlying collection the last element returned by

calling this method.modified while the iteration is in progress in any way other than byThe behavior of an iterator is unspecified if the underlying collection is