
































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!

































interface List { public void add (A x); public Iterator iterator (); interface} Iterator { public A next (); public boolean hasNext ();
interface List { public void add (Object x); public Iterator iterator (); interface} Iterator { public Object next (); public boolean hasNext ();
integer list List xs = new LinkedList(); xs.add(new Integer(0)); Integer x = xs.iterator().next(); // space string list list List ys = new LinkedList(); ys.add("zero"); List> yss = new LinkedList>(); yss.add(ys); String y = yss.iterator().next().iterator().next(); // space string list treated as integer list Integer z = ys.iterator().next(); // compile-time error
class LinkedList implements List { protected class Node { Object elt; Node next; Node (Object e) { elt=e; next=null; } protected} Node h, t; public LinkedList () { h=new Node(null); t=h; } public void add (Object elt) { t.next=new Node(elt); t=t.next; } public Iterator iterator () { return new Iterator () { protected Node p=h.next; public boolean hasNext () { return p!=null; } public Object next () { Object e=p.elt; p=p.next; return e; }
class Lists { public static Object first (List xs) { return xs.iterator().next();
Listspace} ys = new LinkedList(); ys.add("zero"); List yss = new LinkedList>(); yss.add(ys); String y = (String)Lists.first((List)Lists.first(ys s));
interface Comparable { public int compareTo (Object that); class} Byte implements Comparable { private byte value; public Byte (byte value) { this.value = value; } public byte byteValue () { return value; } public int compareTo (Byte that) { // overloading return this.value
that.value; public} int compareTo (Object that) { // overriding
bridge return this.compareTo((Byte)that);
interface Comparable { public int compareTo (A that); class} Byte implements Comparable { private byte value; public Byte (byte value) { this.value = value; } public byte byteValue () { return value; } public int compareTo (Byte that) { return this.value
that.value;
class Lists { public static > A max (List xs) { Iterator xi = xs.iterator();
xi.next(); while (xi.hasNext()) {
xi.next(); if (w.compareTo(x) <
x; return} w;
List} xs = new LinkedList(); xs.add(new Byte(0)); Byte x = Lists.max(xs); List ys = new LinkedList(); ys.add(new Boolean(false)); Boolean y = Lists.max(ys); // compile-time error