1

I am trying to convert an ArrayList to an Array using toArray(), but it returns an object[] instead of double[]. See below:

double [] arr = arrList.toArray(new Double[arrList.size()]);

I'm getting an Incompatible Types error.

This is what the arrList contains:

I/System.out: [0.0, 2.455430030822754, 1.834529995918274, 0.7368429899215698, -0.5264459848403931, -1.5101100206375122, -1.8526500463485718, -1.6131700277328491, -0.9388419985771179, 0.053711701184511185, 0.35541099309921265, -0.18560799956321716, -0.884518027305603, -1.4304900169372559, -1.5486400127410889, -0.943471014499, 0.0]

This is how it is obtained:

private void rec(SensorEvent event) {

    double x = event.values[0];

    arrList.add(x);

}

I've tried without sizing the array (i.e. using new Double[0]) although I didn't think it would be the problem but that did not work either.

I looked for similar Qs online and all I found was on String examples, and I'm thinking Double might be the problem? Not too sure.

Quite new at this so apologies if obvious.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
pizza
  • 35
  • 6
  • @Code-Apprentice That has now been resolved – Noah Feb 06 '20 at 17:31
  • @ItsLogic, explain. I haven't heard of array unboxings (and that'd be a bad feature for performance reasons). – M. Prokhorov Feb 06 '20 at 17:34
  • @Code-Apprentice yes but toArray() cannot be applied to double[] – pizza Feb 06 '20 at 17:34
  • @M.Prokhorov I was referring to that an edit was made to the post changing double[] to Double[] – Noah Feb 06 '20 at 17:35
  • @ItsLogic, I see. I thought you referred to the first comment. – M. Prokhorov Feb 06 '20 at 17:38
  • @pizza That's fine. You call `toArray()` correctly. But you assign the `Double[]` that it returns to a `double[]`. Just fix the declaration as I describe in my answer. – Code-Apprentice Feb 06 '20 at 17:38
  • It appears there's some confusion over what the actual problem is. Please [edit] your question to add a [mre] (← read the linked page) demonstrating the problem, along with the _exact_ error messages. – Slaw Feb 06 '20 at 17:44

2 Answers2

8
double [] arr = arrList.toArray(new Double[arrList.size()]);

double[] is a different type than Double[]. While Java can convert between double and Double with autoboxing and autounboxing, it cannot convert from Double[] to double[]. You can fix the problem with:

Double [] arr = arrList.toArray(new Double[arrList.size()]);
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thank you. I am still getting an incompatible types error though, not too sure why unfortunately. – pizza Feb 06 '20 at 17:38
  • 1
    @pizza, then edit your question to show how you construct your `arrList` variable. – M. Prokhorov Feb 06 '20 at 17:39
  • I've edited my question. Might be because I define the entries in the arrList as double? – pizza Feb 06 '20 at 17:42
  • @pizza Are you saying you're using a `List`? Because that's illegal in Java—primitives cannot be used as generic type arguments. However, `List` is perfectly legal. And when using a `List` you can add primitive `double` elements to it with ease due to autoboxing. – Slaw Feb 06 '20 at 17:45
  • thank you @Slaw. I now get Double[] but wanted to get double[] so I could use in DoubleFFT_1D.realforward(double[] a). Not sure if there is a way to do that? – pizza Feb 06 '20 at 18:12
  • 1
    @pizza Check out https://stackoverflow.com/questions/1109988/how-do-i-convert-double-to-double – Slaw Feb 06 '20 at 18:14
  • @pizza Check the question Slaw posted and if you still need help, post a new question. – Code-Apprentice Feb 06 '20 at 22:39
6

You can use:

Double[] array = arrList.toArray(Double[]::new);

Note, that method references were introduced in java 8 and the method in java 11. Please refer to Code-Apprentice's answer for java 7 compilant solution.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • Thank you. I am getting "Method references are not supported at language level '7'" though. – pizza Feb 06 '20 at 17:33
  • 1
    @pizza sorry, forgot to mention, method references were introduced in java 8, please refer to Code-Apprentice's answer for java 7 compilant solution – Andronicus Feb 06 '20 at 17:34
  • 2
    @pizza, the most important part of this answer is first `Double[]`. – M. Prokhorov Feb 06 '20 at 17:34
  • @M.Prokhorov yes my bad! – pizza Feb 06 '20 at 17:36
  • 2
    Note that [`Collection#toArray(IntFunction)`](https://docs.oracle.com/en/java/javase/13/docs/api/java.base/java/util/Collection.html#toArray(java.util.function.IntFunction)) was added in _Java 11_. – Slaw Feb 06 '20 at 17:48