Search code examples
javadoubleshort

In Java, I want to convert a short[] to a double[]


I am using a short[] array:

short[] buffer = new short[bufferSize];

I have a method that takes as a parameter type double[], so I cannot pass it as-is. I need to make it into a double[]. The best thing that I've done is to make a new object and loop through and convert, like this:

double[] transformed = new double[bufferSize];

for (int j=0;j<bufferSize;j++) {
    transformed[j] = (double)buffer[j];
}

I have not yet even tested the above approach, but I am wondering if there is a better way to do this?

Thanks.


Solution

  • That's as good as it gets, though you can just use buffer.length instead of buffersize.