1

This is my code :

package ehsan;

import java.util.*;
import java.util.stream.Collectors;

public class Test {

    public static void main(String[] args) {
        double[] nums = new Random().doubles(120).toArray();
        System.out.println(Arrays.asList(nums));
    }
}

And the result I get :

[[D@3d9750]

Apperantly Arrays.asList generates a List of double[] and not double. Consequently, How can I (possibly) specify nums I am supplying to asList is a vararg not a single T?

According to the question which is suggested as the original, what changes should I make to the code?

  • You can't - there's no such thing as a `List`. I'm sure this is a dupe, but I may not have time to find it... – Jon Skeet Jul 26 '16 at 12:55
  • @JonSkeet, Come on mate, this differs from that... –  Jul 26 '16 at 12:58
  • I recently gave an answer that explains this behavior here: http://stackoverflow.com/a/38397866/4793951 - There is no real shortcut to converting your `double[]` to `Double[]`, however...Maybe use streams to do it quickly? – Zircon Jul 26 '16 at 12:59
  • @ehsan: I really don't think it does. Both questions are taking a `double[]` and trying to pass it into `Arrays.asList`, expressing surprise at the result type, basically. Where do you think the difference is? Which aspect of your question is not explained in the answers to that question? – Jon Skeet Jul 26 '16 at 13:01
  • @Zircon, thanks for the point, How can I conver `DoubleStream` to `Double[]`, is there `collect()` method available? –  Jul 26 '16 at 13:01
  • @JonSkeet, Sure, But I am confused, what should I change in my code to make it work? –  Jul 26 '16 at 13:02
  • This does both steps at once for you: `List numObjs = Arrays.stream(nums).boxed().collect(Collectors.toList());` – Zircon Jul 26 '16 at 13:02
  • @ehsan: "make it work" requires knowing what you're trying to achieve, which is hard to know here. Your existing code doesn't seem to try to be creating a `Double[]`... – Jon Skeet Jul 26 '16 at 13:03
  • @JonSkeet, all I am trying here is to convert `double[]` to `List` to print it simply :) –  Jul 26 '16 at 13:04
  • If you only need to print it, why not use `Arrays.toString(double[])`? – Jon Skeet Jul 26 '16 at 13:04
  • @Zircon, is there any difference between `Stream.of` and `Arrays.stream`? –  Jul 26 '16 at 13:05
  • `Stream.of` causes the same issue you faced here originally. See: http://stackoverflow.com/a/27888447/4793951 – Zircon Jul 26 '16 at 13:07
  • Thanks, Didn't know these delicate differences between `Stream.of` and `Arrays.stream` .... –  Jul 26 '16 at 13:09
  • @Zircon, just one another question, Is there any difference between `DoubleStream.of` and `Arrays.stream`? –  Jul 26 '16 at 14:48
  • @ehsan Since they both would return a `DoubleStream` in this case, it seems there isn't. I myself still prefer `Arrays.stream` because I find it more readable, though. – Zircon Jul 26 '16 at 15:24

0 Answers0