开发者

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 Doubles. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜