



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Professor: Sussman; Class: PROG LANG TECH & PDGMS; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;
Typology: Study notes
1 / 6
This page cannot be seen from the preview
Don't miss anything!




1
3
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);
class
Queue
Element dequeue() throws...
EmptyQueueException
if (theQueue
== null)
throw
new EmptyQueueException();
Element
e = theQueue.elt;
theQueue
= theQueue.next;
return e;
5
7
13
private static
void
useWhileLoop(
Collection
aFlavours
Iterator
= 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() );
15
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;
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
class
QueueIterator
implements
Iterator
Entry
rest;
QueueIterator(Queue
q)
{
copy
q.theQueue to
rest
class
Queue
int...
modCount
void enqueue(Element
e) {
modCount++;
Element
dequeue()
{ ... modCount++;
19
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?
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