












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!













3 4
7
CMSC 433, Spring 2008 8
class IntegerStack { class Entry { Integer elt; Entry next; Entry(Integer i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Integer i) { theStack = new Entry(i, theStack); } Integer pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Integer i = theStack.elt; theStack = theStack.next; return i; }}}
9
10
13
IntegerStack is = new IntegerStack(); Integer i; is.push(new Integer(3)); is.push(new Integer(4)); i = is.pop();
14
class Stack { class Entry { Object elt; Entry next; Entry(Object i, Entry n) { elt = i; next = n; } } Entry theStack; void push(Object i) { theStack = new Entry(i, theStack); } Object pop() throws EmptyStackException { if (theStack == null) throw new EmptyStackException(); else { Object i = theStack.elt; theStack = theStack.next; return i; }}}
15
Stack is = new Stack(); Integer i; is.push(new Integer(3)); is.push(new Integer(4)); i = (Integer) is.pop();
16
19
Stack is = new Stack(); Integer i; is.push(new Integer(3)); is.push(new Integer(4)); i = is.pop();
20
21
22
25
26
** int count(Collection c) { int j = 0; for (Iterator i = c.iterator(); i.hasNext(); ) { T e = i.next(); j++; } return j;}**
27
int count(Collection c) { int j = 0; for (Iterator i = c.iterator(); i.hasNext(); ) { Object e = i.next(); j++; } return j; } 28
31
void drawAll(Collection c) { for (Shape s : c) s.draw(); } 32
void foo(Collection c) { c.add(new Circle()); }
33
void foo(Collection c) { c.add(new Circle()); c.add(new Shape()); // fails } 34 **public interface Comparable { int compareTo(T o); } // e.g., Boolean implements Comparable public static > void sort(List list) { Object a[] = list.toArray(); Arrays.sort(a); ListIterator i = list.listIterator(); for(int j=0; j 37
38
39