If you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation.
Double[] doubles;
...
double[] d = ArrayUtils.toPrimitive(doubles);
There is also the complementary method
doubles = ArrayUtils.toObject(d);
Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array is really big you shouldn't worry about it. Test it first to see if it is a problem before refactoring.
Implementing the method you had actually asked about would give something like this.
double[] getDoubles(int columnIndex) {
return ArrayUtils.toPrimitive(data[columnIndex]);
}
Answer from Rich Seller on Stack OverflowIf you don't mind using a 3rd party library, commons-lang has the ArrayUtils type with various methods for manipulation.
Double[] doubles;
...
double[] d = ArrayUtils.toPrimitive(doubles);
There is also the complementary method
doubles = ArrayUtils.toObject(d);
Edit: To answer the rest of the question. There will be some overhead to doing this, but unless the array is really big you shouldn't worry about it. Test it first to see if it is a problem before refactoring.
Implementing the method you had actually asked about would give something like this.
double[] getDoubles(int columnIndex) {
return ArrayUtils.toPrimitive(data[columnIndex]);
}
In Java 8, this is one-liner:
Double[] boxed = new Double[] { 1.0, 2.0, 3.0 };
double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();
Note that this still iterates over the original array and creates a new one.
cannot convert from double to double[]?
Convert java double to Double object Example July 14, 2019 4 Comments 1 Min Read 1 2 3 Convert double to Double object Example This example shows how a double primitive value can be converted to a Double object */ 4 5 6 public class JavadoubleToDoubleExample { 7 8 public static void main(String[] args) { double d = 10.56; 9 10 11 12 Use Double constructor to
Double[] instead of double[] variable declarator
[Java] Possible lossy conversion from double to int error.
If you want to explicitly tell the compiler that you want to convert the numbers to integers, you should cast them as such. Example: int x = (int)(10.0/2.0) will make x have a value of 5.
Do note, however, that in the case of int birthrate, the operation 1.0/7.0 will simply be 0 when cast to an int.
Videos
What is a tip to resolve this.
I have a scanner input and im trying to assign the variable in a for loop to it as it but it shows the error(cannot).
double[] amounts = new double[MAXNUM];
amounts=sc.nextDouble();