package listen_sortieren_suchen; /* AUFGABE 63 (List) + 64 (Iterator) */ import java.util.Iterator; import java.util.NoSuchElementException; // ggf. in next() geworfen public class List implements Iterable { private Entry first; // vorderstes Element (Knoten), ist anfangs null /** Innere Klasse (entspricht Listenelement aus Aufgabe 62) */ private class Entry { // E könnte anders genannt werden E elem; // Inhalt des Listenknotens Entry next; public Entry(E e) { elem = e; next = null; // optional (ist implizit null) } } public void add(E element) { if (first == null) { // Liste noch leer first = new Entry(element); } else { //for (int i = 0; i != array.length; i = i + 1) //for (Entry e = first; e.next != null; e = e.next) {} Entry current = first; while (current.next != null) current = current.next; // an dieser Stelle ist in current das letzte Listenelement gespeichert current.next = new Entry(element); } } public E removeFirst() { if (first == null) return null; // nicht genau definiert E element = first.elem; first = first.next; return element; } public int size() { int counter = 0; for (Entry e = first; e != null; e = e.next) counter++; return counter; } public String toString() { String output = ""; Entry e = first; while (e != null) { output += e.elem.toString(); if (e.next != null) output += ", "; e = e.next; } return output; } public boolean remove(Object o) { Entry current = first; Entry previous = null; // zusätzlich zu current noch immer dessen Vorgänger speichern (erleichtert das Entfernen) while (current != null) { if (current.elem == o) { // current speichert in diesem Moment das zu entfernende Element previous.next = current.next; // Zeiger umsetzen (entspricht dem Entfernen) return true; // erfolgreich gelöscht } previous = current; // previous weiterzählen, indem es current speichert bevor current weitergezählt wird current = current.next; // current auf das nächste Element setzen (= Weiterzählen) } // -> Schleife nie mit return true verlassen -> zu entfernendes Element nicht in Liste enthalten return false; } public boolean equals(Object o) { if (!(o instanceof List)) return false; Entry currentThis = this.first; Entry currentOther = ((List) o).first; while (currentThis != null && currentOther != null) { if (currentThis.elem == null) { if (currentOther.elem != null) return false; } else if (currentThis.elem.equals(currentOther.elem) == false) { return false; } currentThis = currentThis.next; currentOther = currentOther.next; } if (currentThis == null && currentOther == null) return true; return false; // nur eine von beiden zu Ende } /* AUFGABE 64: Implementierung des Iterators */ /** Gibt ein Iterator-Objekt zurück (Methode aus dem Interface Iterable) */ public Iterator iterator() { return new MyIterator<>(first); } public class MyIterator implements Iterator { // eigene Iterator-Implementierung (innere Klasse) private Entry next; public MyIterator(Entry head) { next = head; } /** Gibt true zurück, wenn es noch ein Element gibt (vgl. Interface Iterator) */ public boolean hasNext() { if (next != null) return true; return false; } /** Gibt das nächste Element zurück und zählt weiter (vgl. Interface Iterator) */ public E next() { if (!hasNext()) throw new NoSuchElementException(); E elem = next.elem; next = next.next; return elem; } } /* TEST */ public static void main(String[] args) { class Color { public String title = "transparent"; public Color(String t) { title = t; } public String toString() { return title; } } List colorList = new List<>(); // Liste von Farben (Objekte) //LinkedList colorList = new LinkedList<>(); colorList.add(new Color("green")); colorList.add(new Color("red")); colorList.add(new Color("orange")); colorList.add(new Color("olive")); colorList.add(new Color("black")); System.out.println("Ausgabe 1: " + colorList); Color blue = new Color("blue"); colorList.remove(new Color("blue")); // entfernt blue NICHT, da neues Objekt != c System.out.println("Ausgabe 2: " + colorList); colorList.remove(blue); // entfernt blue (-> Refrenzgleichheit) System.out.println("Ausgabe 3: " + colorList); // Iterator-Test: for (Color c : colorList) { // for-each durch "implements Iterable" nun möglich) System.out.println("Farbe: " + c); } } }