gpt4 book ai didi

java - 将 dataset.addSeries 与 Long[] 值一起使用

转载 作者:行者123 更新时间:2023-11-30 04:25:10 25 4
gpt4 key购买 nike

dataset.addSeries 采用的参数如下(java.lang.Comparable key,
double[] values,
int bins,
double minimum,
double maximum)

现在,我正在尝试使用一个名为 Long[] v1 的变量在 double[] values字段,无法弄清楚如何转换它。

最佳答案

摘自 Jon Skeet 在 How to convert array of floats to array of doubles in Java? 上的回答,我引用:

Basically something has to do the conversion of each value. There isn't an implicit conversion between the two array types because the code used to handle them after JITting would be different - they have a different element size, and the long would need a conversion whereas the double wouldn't. Compare this to array covariance for reference types, where no conversions are required when reading the data (the bit pattern is the same for a String reference as an Object reference, for example) and the element size is the same for all reference types.

In short, something will have to perform conversions in a loop. I don't know of any built-in methods to do this. I'm sure they exist in third party libraries somewhere, but unless you happen to be using one of those libraries already, I'd just write your own method.

以下是乔恩答案的改编实现,以适应您的问题:

public static double[] convertLongsToDoubles(Long[] input)
{
if (input == null)
{
return null; // Or throw an exception - your choice
}
double[] output = new double[input.length];
for (int i = 0; i < input.length; i++)
{
output[i] = input[i];
}
return output;
}

关于java - 将 dataset.addSeries 与 Long[] 值一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16092068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com