Come non detto, la classe TheComparator mi viene compilata ma testandola mi da questo errore:Hit uncaught exception java.lang.ClassCastException
Questa è la classe TheComparator:
codice:
public class TheComparator implements Comparator{
Object ea, eb, keya, keyb;
//Compariamo gli elementi in base al loro valore di key
private void getXY(Object a, Object b) {
if(a==null || b==null)
throw new InvalidElementException("Argomento nullo");
try {
keya = ((Item) a).key();
ea = ((Item) a).element();
keyb = ((Item) b).key();
eb = ((Item) b).element();
}
catch (ClassCastException e)
{ throw new InvalidElementException("Argomento non è un Item");}
}
public boolean isLessThan(Object a, Object b) {
getXY(a, b);
return (((Integer) keya).intValue() < ((Integer) keyb).intValue());
}
public boolean isLessThanOrEqualTo(Object a, Object b) {
getXY(a, b);
return (((Integer) keya).intValue() <= ((Integer) keyb).intValue());
}
......
a e b sono i due oggetti Item, per cui dal debug leggo che le variabili keya e keyb contengono i valori, "43" e "42", ma
codice:
public boolean isLessThanOrEqualTo(Object a, Object b) {
getXY(a, b);
return (((Integer) keya).intValue() <= ((Integer) keyb).intValue()); }
La linea in grassetto crea l'eccezione perchè probabilmente non può esser fatto il casting.
cos'è che non va?