1

The arguments that dataset.addSeries takes are as follows (java.lang.Comparable key, double[] values, int bins, double minimum, double maximum)

Right now, I am trying to use a variable called Long[] v1 in the double[] values field and cannot figure out how to convert it.

user2007843
  • 609
  • 1
  • 12
  • 30

2 Answers2

1

From Jon Skeet's answer on How to convert array of floats to array of doubles in Java?, I quote:

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.

The following is an adapted implementation of Jon's answer to fit your question:

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;
}
Community
  • 1
  • 1
0x6C38
  • 6,796
  • 4
  • 35
  • 47
  • Plagiarized from this answer by Jon Skeet: http://stackoverflow.com/a/2019374/19679 . If you're going to steal, I guess pull from the best. – Brad Larson Apr 18 '13 at 21:45
1

You're going to have to do this yourself.

Write a method that does the conversion for you.

public static double[] convertFromLongToDouble(Long[] l) {
double[] doubleArray = new double[l.length];
     // .. iterate through the Long array and populate to double array
return doubleArray;
}
Kal
  • 24,724
  • 7
  • 65
  • 65