ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
971
971
EMPFEHLEN
-
Ich soll ein Programm erstellen welches nach verschiedenen Attribute sortiert:
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
public static void main(String[] args) { List<Comparator> comp = new ArrayList<Comparator>(); comp.add(new NummerSortieren()); comp.add(new NameSortieren()); comp.add(new AlterSortieren()); comp.add(new NoteSortieren()); Student[] studis = { new Student(4, "Archimedes Syrakus", 2296, 6.0), new Student(3, "Fritzli Schneider", 9, 5.9), new Student(1, "Walterli Tell junjun.", 12, 4.3), new Student(6, "Greengrey vonRotz", 20, 3.7), new Student(5, "Maria Stuart", 467, 2.9) }; for (Comparator curComp: comp) { System.out.println("nach Notendurchschnitt sortiert:"); System.out.println("-----------"); for (Student alle : studis) { System.out.println(alle); } Arrays.sort(studis, comp); System.out.println("Sortiert:"); System.out.println("-----------"); for (Student alle : studis) { System.out.println(alle); } } System.out.println("-----------"); System.out.println("Process completed."); }
Das sortieren nach einem Attribut ghet, wenn ich jedoch nach jedem sortieren lassen wurd und wieder dann nacheinander ausgeben will, erhalte (Code oben) ich folgenden Fehler:
Code :1 2 3 4
Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (Student[], List<Comparator>) at Student.main(Student.java:40)
Was ich feststellen konnte:
Code :1
Comparator is a raw type. References to generic type Comparator<T> should be parameterized
Kann mir jemand da weiterhelfen kann?

-
-
Hi,
erstmal warum der Fehler erscheint!
Du übergebst in der Zeile
eine Liste von Comperators... der möchte nacheinander einzelne Comperators. also muss dortCode :1
Arrays.sort(studis, comp)
stehen.Code :1
Arrays.sort(studis, curComp)
Zur um die rawType Warnungen zu killen, hab ich dir mal den Code schnell reworked
Code :1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Comparator; public class Test { public static void main(String[] args) { List<Comparator<Student>> comp = new ArrayList<Comparator<Student>>(); comp.add(new NummerSortieren()); comp.add(new NameSortieren()); comp.add(new AlterSortieren()); comp.add(new NoteSortieren()); Student[] studis = { new Student(4, "Archimedes Syrakus", 2296, 6.0), new Student(3, "Fritzli Schneider", 9, 5.9), new Student(1, "Walterli Tell junjun.", 12, 4.3), new Student(6, "Greengrey vonRotz", 20, 3.7), new Student(5, "Maria Stuart", 467, 2.9) }; for (Comparator<Student> curComp: comp) { System.out.println("nach Notendurchschnitt sortiert:"); System.out.println("-----------"); for (Student alle : studis) { System.out.println(alle); } Arrays.sort(studis, curComp); System.out.println("Sortiert:"); System.out.println("-----------"); for (Student alle : studis) { System.out.println(alle); } } System.out.println("-----------"); System.out.println("Process completed."); } }
Nun musst du nur noch die selbstgeschriebenen einzelnen Comperatoren abwandeln nach dem Schema
Code :1 2 3 4 5 6 7 8 9 10 11 12
import java.util.Comparator; public class NameSortieren implements Comparator<Student> { @Override public int compare(Student o1, Student o2) { // sortieralgorithmus return 0; } }
Ich hoffe ich konnte dir weiterhelfen.
Greaz
Ähnliche Themen
-
Objekte in einem Array sortieren
Von greenslot im Forum PHPAntworten: 5Letzter Beitrag: 07.12.10, 15:32 -
Problem mit Comparator
Von plueschi im Forum Java GrundlagenAntworten: 3Letzter Beitrag: 07.05.09, 08:10 -
Objekte nach einem ihrer Attribute sortieren
Von geisendorf im Forum JavaAntworten: 3Letzter Beitrag: 23.01.08, 09:12 -
Objekte in Array sortieren mit Rekursion
Von ToNie im Forum JavaAntworten: 9Letzter Beitrag: 18.10.07, 11:51 -
Vector Objekte sortieren
Von flush83 im Forum JavaAntworten: 1Letzter Beitrag: 10.01.07, 23:07





Zitieren

Login





