2

Given a method definition:

public void create(double... values);

This method is called like this:

List<Double> values = ...;
Double[] doubles = values.toArray(new Double[0]);
create(doubles);  // wont work

Why does it not work?

helt
  • 4,925
  • 3
  • 35
  • 54
  • 3
    Overload the method (`void create(Double... values)`). `Double[]` and `double[]` cannot be converted into each other. – Turing85 Nov 01 '16 at 08:05
  • 3
    You need to manually convert `Arrays.stream(input).mapToDouble(Function.identity()).toArray()`. – Boris the Spider Nov 01 '16 at 08:05
  • well, the method above is the only means to do it, no other method is available – helt Nov 01 '16 at 08:06
  • Note: `double[]` (array of primitives) is not the same as `Double[]` (array of references) To create a `double[]` from a `Double[]` you have to create a new array and copy all the values out of it. Best to avoid creating arrays of wrappers in the first place. – Peter Lawrey Nov 01 '16 at 08:06
  • 1
    @Turing85 but then you're still in the same pickle - you need to convert `Double[]` to `double[]` in the method overload. – Boris the Spider Nov 01 '16 at 08:07
  • @BoristheSpider true that. But the question is: what does the method do? If it iterates over the varargs and does something with each value, autoboxing will take care of that. – Turing85 Nov 01 '16 at 08:10
  • @Turing85 you're not suggesting simply copying the code from one method to another are you? I mean, if there are two lines in the method then maybe; but what if there are 5 lines? 10? Where do you draw the line...? – Boris the Spider Nov 01 '16 at 08:11
  • ok. so the problem *is not* `double[] -> double...`, the problem is `Double[] -> double[]`. Check. About the example above: Its a stripped-down version of the actual code i have. the method does not return `void`, but an actual object, but it doesn't matter wrt the question. – helt Nov 01 '16 at 08:13
  • 1
    @BoristheSpider I say that no further speculation is of use as long as the functionality of the method is unknown – Turing85 Nov 01 '16 at 08:13
  • @Turing85 but what else is there to do on a Tuesday morning? You don't mean _work_ do you. Eugh! – Boris the Spider Nov 01 '16 at 08:18
  • 1
    @helt Don't to `Double[] doubles = values.toArray(new Double[0])`, do `values.stream().map(Double::doubleValue).toArray()` instead. That way you only copy once. Also, never do `.toArray(new Xxxx[0])` as it creates an empty array, then discards it and creates an array of the correct size. You of course know the correct size to begin with so use it. – Boris the Spider Nov 01 '16 at 08:23
  • wow. so many mistakes in 2 loc :) – helt Nov 01 '16 at 08:28
  • 1
    @helt 1) every non-trivial program contains at least one bug. 2) every non-trivial program can be simplified by at least one line of code. Therefore: every non trivial program can be simplified to one line of code. With a bug. – Boris the Spider Nov 01 '16 at 08:30
  • @BoristheSpider at least it isn't monday ;) – Turing85 Nov 01 '16 at 11:17

1 Answers1

2
double[] unboxed = Stream.of(boxed).mapToDouble(Double::doubleValue).toArray();

Credits to: https://stackoverflow.com/a/30117592/3635454

Community
  • 1
  • 1
pleft
  • 7,567
  • 2
  • 21
  • 45
  • Note that this does copy the whole array; which is of course necessary to answer the question as it stands (you have my vote). But it should probably be avoided if possible, as it's really a waste of CPU cycles. – Boris the Spider Nov 01 '16 at 08:22