2016-02-24 12 views
22

Ho riscontrato un problema quando utilizzo TreeMap.Perché è necessario convertire da Integer a int

Map<Integer, Integer> a = new TreeMap<Integer, Integer>(); 
    a.put(5,1); 
    a.put(3,2); 
    a.put(4,3); 
    int target = 7; 
    System.out.println(target - a.get(5)); //line 6 
    for(Map.Entry b : a.entrySet()){ 
     System.out.println(target - b.getValue()); //line 8 
    } 

Il codice sopra riportato mi ha fornito un errore di compilazione. Tuttavia, quando cambio la linea 8 a questa:

Map<Integer, Integer> a = new TreeMap<Integer, Integer>(); 
    a.put(5,1); 
    a.put(3,2); 
    a.put(4,3); 
    int target = 7; 
    System.out.println(target - a.get(5)); //line 6 
    for(Map.Entry b : a.entrySet()){ 
     System.out.println(target - (int) b.getValue()); //line 8 
    } 

Quindi funziona. Qualcuno potrebbe darmi qualche idea del perché non ho bisogno di alcun cambiamento nella riga 6, ma ho bisogno di convertire un intero in int nella riga 8?

risposta

43

Si è ignorato l'avviso "tipo non elaborato" nell'istruzione for. Dovrebbe essere:

for(Map.Entry<Integer,Integer> b : a.entrySet()) { 
     ... 

Il tipo grezzo causerebbe getValue() per tornare Object. Se fornisci i parametri del tipo, il compilatore sa che restituirà Integer e questo verrà automaticamente rimosso.

+0

Oh !!! Grazie mille! – youngyjd

+1

Hai bisogno di definire il tipo lì o l'operatore diamante (Map.Entry <>) è sufficiente (che è stato introdotto con Java 7) come a.entrySet() già definisce il tipo? L'uso di nulla come nell'OP ovviamente causa l'errore. – Thomas

+2

@Thomas: è necessario definire il tipo. Usare semplicemente l'operatore diamond ti darà un errore in fase di compilazione. –

5

Esistono più operazioni al di sotto di (int) b.getValue(). Il primo getValue() restituisce Object e quindi viene assegnato a Integer, che viene quindi rimosso da int. a.get() nel proprio restituisce immediatamente Integer poiché è stato dichiarato un con Numero intero in <> (vedere https://docs.oracle.com/javase/7/docs/api/java/util/Map.html#get(java.lang.Object) restituisce il tipo V).

Il target - b.getValue() non è stato compilato perché era l'operazione int - Object che non è definita per l'operatore -. Questo è il motivo per cui devi fare il cast a (int).

In seguito non funziona anche se b si riferisce all'oggetto che è Integer.

Integer a = 1; 
Object b = a; 
System.out.println(3 - b); // compile time error "bad operand types for binary operator '-'" 

seguenti lavori

Integer a = 1; 
Object b = a; 
System.out.println(3 - a); 

funziona anche

Integer a = 1; 
Object b = a; 
System.out.println(3 - (int) b); //this is when you say to compiler not to worry since you are sure that object reference refers to the object that is Integer. 

Anche se in fase di esecuzione b non si riferisce in int il cast avrà esito negativo. Anche se è stato compilato in primo luogo.

Integer a = 1; 
String s = "shouldn't work at runtime"; 
Object b = s; 
System.out.println(3 - (int) b); //this will compile but fail at runtime