type safety, conversions

Status
Nicht offen für weitere Antworten.
S

shortStory

Gast
Hallo,

ich habe Problem das folgende Warning aus Eclipse rauszubekommen:

Code:
Type safety: Unchecked invocation sort(List, Comparator) of the generic method sort(List<T>, Comparator<? super T>) of type Collections

Die betroffene Zeile:

Code:
Collections.sort(this.results, comparator);

mir ist bewußt, daß ich this.results mit meinen spezialListentyp versehen sollte ala this.results<MeineListe>, aber was soll ich mit Comparator<? super T> machen? ich hab schon hier im Forum gesucht und auch gegoogled aber nix gefunden was mir weiterhilft...

mfg
shrtStory
 

DaKo

Bekanntes Mitglied
Das <? super T> heißt einfach nur, dass der Comparator irgendeinen Typ haben kann, der entweder T ist oder eine Oberklasse von T
 

hupfdule

Top Contributor
Sowohl this.results, als auch comparator sollten beide generisch sein. Wenn das bei dir nicht der Fall ist, kommt die Warnung.
 
S

shortStory

Gast
Das er den COmparator generisch haben will, ist mir schon klar nur weiß ich nicht wie ich den generisch machen soll...

Das ist ein ganz normaler Comparator...irgendwie steh ich auf nem Schlauch:



Code:
Comparator comparator = new Comparator(){
            public int compare(Object o1, Object o2)
            {
                ConnectionStatisticsObj c1 = (ConnectionStatisticsObj)o1;
                ConnectionStatisticsObj c2 = (ConnectionStatisticsObj)o2;
                
                if (column == null){
                    return 0;
                }
                if (column.equals("date")){
                    ...
                }
                else if (column.equals("sec")){
                    ...
                }
                else if (column.equals("formatTime")){
                   ...
                }
                else if (column.equals("bytes")){
                    ...
                }
                else return 0;
            }
        };

Was für einen Typen kann man nem Comparator geben? dieser Comparator vergleicht alles vom Datum bis zum Long...

mfg
shortStory
 

DaKo

Bekanntes Mitglied
Da du sowieso schon davon ausgehst, dass alle übergebenen Objekte vom Typ ConnectionStatisticsObj sind, gib diesen Datentyp deinem Comparator

Code:
Comparator<ConnectionStatisticsObj> ...
 

hupfdule

Top Contributor
Code:
Comparator<TolleKlasse> comparator = new Comparator<TolleKlasse>(){
            public int compare(TolleKlasse o1, TolleKlasse o2)
            {
               ...
            }
        };

Das wäre ein Comparator, der nur für TolleKlasse zuständig ist.


Was für einen Typen kann man nem Comparator geben? dieser Comparator vergleicht alles vom Datum bis zum Long

Die gemeinsame Basisklasse aller Klassen, deren Objekte du damit vergleichen willst. Dass in diesen Objekten wieder Date und Long enthalten sind, ist erst mal egal. Das beachtest du innerhalb des Comparators. Du kannst dich in diesem Fall aber drauf verlassen, dass die übergebenen Objekte vom Typ TolleKlasse sind und daher die entsprechenden Felder haben.


(In deinem Fall ist TolleKlasse == ConnectionStatisticsObj)
 
S

shortStory

Gast
Hallo nochmal,

danke. Habs verstanden...und werds umgehend ändern :)

mfg
shortStory
 
Status
Nicht offen für weitere Antworten.
Ähnliche Java Themen
  Titel Forum Antworten Datum
F Type safety: A generic array of.. Fehler Java Basics - Anfänger-Themen 3
T Compiler-Fehler Null type safety (type annotations) Java Basics - Anfänger-Themen 5
C Warning: Type safety: Potential heap pollution via varargs parameter array Java Basics - Anfänger-Themen 5
A Variablen Type safety Warnung beseitigen Java Basics - Anfänger-Themen 3
lumo lösen von: "Type safety"? Java Basics - Anfänger-Themen 4
S Type safety bei einer ArrayList Java Basics - Anfänger-Themen 2
S Type safety Java Basics - Anfänger-Themen 6
A java.sql.SQLException: Data type mismatch. Java Basics - Anfänger-Themen 1
I No EJB found with interface of type Java Basics - Anfänger-Themen 12
J Scanner cannot be resolved to a type Java Basics - Anfänger-Themen 3
Denix The public type Main must be defined in its own fileJava(16777541) Java Basics - Anfänger-Themen 13
M Umgang mit dem Type "Date" Java Basics - Anfänger-Themen 6
N Fehler "Cannot instantiate the type" Java Basics - Anfänger-Themen 3
nbergmann The type JOptionPane is not accessible. Java Basics - Anfänger-Themen 4
M Type Erasure in Java Java Basics - Anfänger-Themen 12
Vivien Hilfe bei Type Conversion Java Basics - Anfänger-Themen 3
Flo :3 Variablen Type dismatch: cannot convert from string to int Java Basics - Anfänger-Themen 9
P Jackson mapper.readValue mit generischem Type Java Basics - Anfänger-Themen 10
H Methode mit Array als Rückgabe This method must return a result of Type int[] Java Basics - Anfänger-Themen 2
D Klassen undefined for the type 'object' Java Basics - Anfänger-Themen 2
N The method setSaldo(double) in the type Konto is not applicable for the arguments (int, int) Java Basics - Anfänger-Themen 2
W The type Long is not visible HashMap Java Basics - Anfänger-Themen 4
J-Gallus Ein Getter bekommt eine anderen Type als er Return soll Java Basics - Anfänger-Themen 9
S Fehler: Editor does not contain a main type Java Basics - Anfänger-Themen 3
G Programm wird nicht ausgeführt: Editor does not contain a main type Java Basics - Anfänger-Themen 10
Syncopated Pandemonium Compiler-Fehler The constructor MP3File(File) refers to the missing type NoMPEGFramesException Java Basics - Anfänger-Themen 7
K Operatoren The Operator * is undefined for the argument type(s) double, String Java Basics - Anfänger-Themen 4
K VerketteteListen unexpected type Fehler Java Basics - Anfänger-Themen 2
B OOP Cannot instantiate the type AuDList<Integer> Java Basics - Anfänger-Themen 18
B Methoden The method mirror(double[]) in the type Convolution is not applicable for the arguments (double) Java Basics - Anfänger-Themen 8
I Fehlermeldung: Java does not contain a main type Java Basics - Anfänger-Themen 1
J Fehlermeldung : cannot invoke char(at) int on the primitive type int --- Anfänger Java Basics - Anfänger-Themen 5
V RMI - argument type mismatch Java Basics - Anfänger-Themen 1
S Selection does not contain a main type! Java Basics - Anfänger-Themen 5
R The method printf(String, Object[]) in the type printStrem in not applicable for the arguments ... Java Basics - Anfänger-Themen 3
M "illegal start of type" eindimensionales Schiffe versenken Java Basics - Anfänger-Themen 7
S Vererbung Fehlermeldung: the hierarchy of the type "class name" is inconsistent Java Basics - Anfänger-Themen 10
S Selection does not contain a main type Java Basics - Anfänger-Themen 12
H enum Type Java Basics - Anfänger-Themen 6
K Variablen RETURN in Case-Switch / This method must return a result of type Item Java Basics - Anfänger-Themen 4
P Variablen generic type variablen in object array Java Basics - Anfänger-Themen 1
P enum: cannot be resolved to a type Java Basics - Anfänger-Themen 2
I Erste Schritte Eclipse - Does not contain a main type Java Basics - Anfänger-Themen 8
W Enum Konstruktor Type Java Basics - Anfänger-Themen 2
C MIME-Type null Java Basics - Anfänger-Themen 4
L The operator && is undefined for the argument type(s) String, boolean Java Basics - Anfänger-Themen 8
G default class type Java Basics - Anfänger-Themen 3
J Type inference Java Basics - Anfänger-Themen 26
Joew0815 Compiler-Fehler Unexpected Type - Problem mit Variablen rechnen Java Basics - Anfänger-Themen 7
T selection method does not contain a main type Java Basics - Anfänger-Themen 7
O unexpected type - weiß nicht weiter! Java Basics - Anfänger-Themen 3
M Datentypen problem!! Unexpected type in bleuj Java Basics - Anfänger-Themen 2
P Compiler-Fehler unexpected type - Stehe auf dem Schlauch Java Basics - Anfänger-Themen 3
M This method must return a result of type int Java Basics - Anfänger-Themen 13
T Unhandled exception type Java Basics - Anfänger-Themen 2
J unexpected type variable/value Java Basics - Anfänger-Themen 2
M Collections mit >2 type Parametern? Java Basics - Anfänger-Themen 8
D Compiler-Fehler void is an invalid type for the variable Java Basics - Anfänger-Themen 5
H LocationReferenceImpl cannot be resolved to a type Java Basics - Anfänger-Themen 5
K unexpected type variable/value Java Basics - Anfänger-Themen 7
M Fehlermeldung: the method.... ist undefined for the type object Java Basics - Anfänger-Themen 6
K Erste Schritte selection does not contain a main type Java Basics - Anfänger-Themen 3
M Objekt Cannot instantiate the type ... Java Basics - Anfänger-Themen 10
S this method must return a result of type double Java Basics - Anfänger-Themen 2
L Type/Cast Problem Java Basics - Anfänger-Themen 6
X enum Fehlermeldung "The public type Day must be defined in its own file" Java Basics - Anfänger-Themen 8
B Editor does not contain a main type Java Basics - Anfänger-Themen 3
E Datentypen type cast problem (int, byte,) Java Basics - Anfänger-Themen 5
L Illegal Start of Type, wie finde ich den fehler Java Basics - Anfänger-Themen 4
V Eclipse "Selection does not contain a main type" Java Basics - Anfänger-Themen 13
P BlueJ Fehlermeldung - Illegal Start of Type Java Basics - Anfänger-Themen 8
J Color cannot be resolved to a type Java Basics - Anfänger-Themen 4
B Undefined for the type... Java Basics - Anfänger-Themen 15
D Unhandled Exception type IOException in Constructor Java Basics - Anfänger-Themen 1
B Selection does not contain a main type Java Basics - Anfänger-Themen 2
A The method getYear() from the type Date is deprecated Java Basics - Anfänger-Themen 6
B Type von Class erhalten Java Basics - Anfänger-Themen 2
G incompatibel return type bei vererbung Java Basics - Anfänger-Themen 18
J String cannot be resolved to a type Java Basics - Anfänger-Themen 6
C Editor does not contain a main type Java Basics - Anfänger-Themen 7
J Type-Casting Java Basics - Anfänger-Themen 8
M 'void' type not allowed Java Basics - Anfänger-Themen 18
B method intValue() is undefined for the type String (?) Java Basics - Anfänger-Themen 4
H wieso fehler ? must return a type of int. Java Basics - Anfänger-Themen 4
G Konvertierung String in long type Java Basics - Anfänger-Themen 15
G raw type Java Basics - Anfänger-Themen 2
N Java Sound - Unsupported control type Java Basics - Anfänger-Themen 2
J System.in.read(): Unhandled exception type IOException Java Basics - Anfänger-Themen 12
S falscher return type ? Java Basics - Anfänger-Themen 4
F The type . is ambiguous Java Basics - Anfänger-Themen 2
A Generics Raw Type Java Basics - Anfänger-Themen 9
G Kein main Type? Java Basics - Anfänger-Themen 3
N invalid method declaration; return type required Java Basics - Anfänger-Themen 4
S StringToInt() - Type mismatch: cannot convert from... Java Basics - Anfänger-Themen 16
R Eclipse: This method must return a result of type double. Java Basics - Anfänger-Themen 2
M Fehlermeldung (the method add(OE) in the type Abteilung.) Java Basics - Anfänger-Themen 2
O must be an array type (String array füllen) Java Basics - Anfänger-Themen 7
M method xxx() from the type xxx is deprecated Java Basics - Anfänger-Themen 3
G typeof is undefined for the type "Klassenname" Java Basics - Anfänger-Themen 8
A The method getClass() from the type saveLog is not static Java Basics - Anfänger-Themen 2

Ähnliche Java Themen

Neue Themen


Oben