1

I'm using a function (which I don't own so can't change its return type) that returns a double[]

I'm trying to create a java.util.List<java.lang.Double> from it.

If only the function returned a Double[], then I could use Arrays.asList(...) but that doesn't work for double[].

Am I missing something simple here?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78
  • Google Guava has a helper for that - "List asList(prim... backingArray) Wraps a primitive array as a List of the corresponding wrapper type. " – Gyro Gearless Nov 19 '15 at 08:41

2 Answers2

1

You will unfortunately need to iterate through the entire array and create a new double[] array from scratch.

Look up boxing and unboxing to learn more about this phenomenon.

Per answer here: How do I convert Double[] to double[]?

Community
  • 1
  • 1
jiaweizhang
  • 809
  • 1
  • 11
  • 28
1

In Java 8 you can use streams to convert the array:

double[] d = ...;
List<Double> d2 = DoubleStream.of(d).boxed().collect(Collectors.toList());
wero
  • 32,544
  • 3
  • 59
  • 84