H
Hartmut
Gast
ich muss nen Index bzw Sachwortverzeichnis eines Buches als Klasse implementieren. das heißt, eine Datenstruktur, die <begriff>: <seitenzahl 1> <seitenzahl 2> … <seitenzahl n> speichert.
folgendes habe ich bereits:
und zum Testen:
als Ausgabe bekomme ich:
[Schlagwort: 15, Schlagwort: 3, Test: 1]
[Demo: 1, Schlagwort: 15, Schlagwort: 3, Schlagwort: 7, Test: 1]
dabei sollte es so sein:
[Schlagwort: 3 15, Test: 1]
[Demo: 1, Schlagwort: 3 7 15, Test: 1]
was mache ich falsch??
folgendes habe ich bereits:
Java:
public class Index {
private ArrayList<String> index;
public Index() {
index = new ArrayList<String>();
}
public Index(String begriff, int seite) {
this.add(begriff, seite);
}
public Index(Index i) {
this.index = (ArrayList<String>) i.index.clone();
}
public void add(String begriff, int seite) {
if (!this.index.contains(begriff + ": " + seite)) {
this.index.add(begriff + ": " + seite);
}
Collections.sort(this.index);
}
public String toString() {
return "" + index;
}
}
und zum Testen:
Java:
public static void main(String[] args) {
Index index = new Index();
index.add("Test", 1);
index.add("Schlagwort", 15);
index.add("Schlagwort", 3);
index.add("Schlagwort", 15);
Index index2 = new Index(index);
index2.add("Demo", 1);
index2.add("Schlagwort", 7);
System.out.println(index);
System.out.println(index2);
}
als Ausgabe bekomme ich:
[Schlagwort: 15, Schlagwort: 3, Test: 1]
[Demo: 1, Schlagwort: 15, Schlagwort: 3, Schlagwort: 7, Test: 1]
dabei sollte es so sein:
[Schlagwort: 3 15, Test: 1]
[Demo: 1, Schlagwort: 3 7 15, Test: 1]
was mache ich falsch??