/** * Created on 2006-12-13 */ import java.util.Iterator; /** * Eine einfach verkettete Liste mit Operationen zum einfuegen (hinten) * und zurueckliefern des vordersten Elements, die einen Iterator auf sich * zurueck liefern kann. * * @author Frank Blendinger (fb@intoxicatedmind.net) * * @param Typarameter * */ public class SimpleList implements Iterable { private SimpleListElem first; public SimpleList() { this.first = null; } public T first() { if (this.first == null) { return null; } return this.first.getData(); } public void insert(T el) { SimpleListElem neu = new SimpleListElem(el); if (this.first == null) { this.first = neu; return; } SimpleListElem cur = this.first; while (cur.getNext() != null) { cur = cur.getNext(); } cur.setNext(neu); } public Iterator iterator() { return new SimpleListIterator(this.first); } public static void main(String[] args) { SimpleList textListe = new SimpleList(); String[] einzufuegen = {"foo", "bar", "baz", "quz", "42"}; // for-each-Schleife ueber ein Array for (String s : einzufuegen) { textListe.insert(s); } // for-each-Schleife ueber ein Iterable-Objekt for (String eintrag : textListe) { System.out.println(eintrag); } } }