interface List { public void add (Object x); public Iterator iterator (); } interface Iterator { public Object next (); public boolean hasNext (); } class LinkedList implements List { ... } class Test { public static void main (String[] args) { // byte list List xs = new LinkedList(); xs.add(new Byte(0)); xs.add(new Byte(1)); Byte x = (Byte)xs.iterator().next(); // string list List ys = new LinkedList(); ys.add("zero"); ys.add("one"); String y = (String)ys.iterator().next(); // string list list List zss = new LinkedList(); zss.add(ys); String z = (String)((List)zss.iterator().next()).iterator().next(); // string list treated as byte list Byte w = (Byte)ys.iterator().next(); // run-time exception } }
Example 1: List and iterator interfaces.