What does keySet().toArray(new Double[0]) do?
What do开发者_开发问答es the following return do? And how can I use that value:
private Map<Double, String> theLabels = new HashMap<Double, String>();
public Double[] getTheLabels() {
return theLabels.keySet().toArray(new Double[0]);
}
Is this correct?
Double[] aD = theClassInQuestion.getTheLabels();
Thanks in advance.
HJW
It is correct, however, unless the theLabels is empty, the reallocation of the array is forced. Here is an alternative:
public Double[] getTheLabels() {
return theLabels.keySet().toArray(new Double[theLabels.size()]);
}
It converts the set of keys into an array of Double
s. The parameter new Double[0]
is needed for the compiler/JVM to infer the correct type for the array to be returned. (If the array parameter is long enough to hold all key values, it will be used, otherwise a new array of the same runtime type will be created with the required length.)
Here is the Javadoc.
Your assignment of the return value is correct.
精彩评论