Guardando java api per il framework java collections, non ho trovato il metodo toArray() in HashSet, c'è il metodo toArray() nella classe astratta Set.Come convertire hash Impostare in array usando toArray() se il metodo toArray non è specificato?
class Ideone {
public static void main (String[] args) throws java.lang.Exception {
Set x = new HashSet();
x.add(4);
//ArrayList<Integer> y = x.toArray(); this does not work !
int[] y = x.toArray();//this does not work!
System.out.println(x.toArray());//this gives some weird stuff printed : Ljava.lang.Object;@106d69c
}
}
Come si converte hashset in array se non è specificato toArray()?
beh, il toArray restituisce un 'oggetto [] ', solo la stampa che ti dà il tipico output java se toString non viene sovrascritto correttamente. E un 'Object []' non è implicitamente convertibile in 'int []'. – luk2302
@ luk2302 esattamente. Questo è ciò che ho scritto nella mia risposta, ma è stato downvoted. Non sono sicuro di quale sia il problema in risposta. Qualcuno può spiegare per favore in modo che io possa migliorare –
@MSach probabilmente perché si spiega solo l'output stampato, non perché la prima riga non funziona né quale sarebbe il modo corretto - ma ovviamente è solo un'ipotesi – luk2302