Probably a good idea.
An example adapted from the GJ Tutorial by Gilad Bracha, et al:
interface Collection<A> {
public void add(A x);
public Iterator<A> iterator();
}
interface Iterator<> {
public A next();
public boolean hasNext();
}
class LinkedList<A> implements Collection<A> {
protected class Node {
A elt;
Node next = null;
Node (A elt) { this.elt=elt; }
}
protected Node head = null
protected Node tail = null;
public LinkedList () {}
public void add (A elt) {
if (head==null) { head=new Node(elt); tail=head; }
else { tail.next=new Node(elt); tail=tail.next; }
}
public Iterator<A> iterator () {
return new Iterator<A> () {
protected Node ptr=head;
public boolean hasNext () { return ptr!=null; }
public A next () {
if (ptr!=null) {
A elt=ptr.elt; ptr=ptr.next; return elt;
}
else {
throw new NoSuchElementException ();
}
}
};
}
Undocumented support for this in Java 1.3!
All the tricks are in the compiler; doesn't change the byte code at all.
May be documented for Java 1.4