interface List<A> {
public void add(A x);
public Iterator<A> iterator();
}
interface Iterator<A> {
public A next();
public boolean hasNext();
}
class LinkedList<A> implements List<A> { ... }
class Test {
public static void main (String[] args) {
// byte list
List<Byte> xs = new LinkedList<Byte>();
xs.add(new Byte(0)); xs.add(new Byte(1));
Byte x = xs.iterator().next();
// string list
List<String> ys = new LinkedList<String>();
ys.add("zero"); ys.add("one");
String y = ys.iterator().next();
// string list list
List<List<String>> zss = new LinkedList<List<String>>();
zss.add(ys);
String z = zss.iterator().next().iterator().next();
// string list treated as byte list
Byte w = ys.iterator().next(); // compile-time error
}
}
Example 2: Lists, iterators, linked lists, and the test class rewritten in GJ.
Back to Article