Convert a double array to Double ArrayList

前端 未结 5 1831
逝去的感伤
逝去的感伤 2020-12-06 00:25

When I try to convert a double array to a Double arrayList I got the following error:

Exception in thread \"main\" java.lang.ClassCastException: [D ca

5条回答
  •  抹茶落季
    2020-12-06 00:53

    Using Java 8 Streams API this is achieved with

    DoubleStream.of(doublesArray).boxed().collect(Collectors.toList());
    

    If returning an ArrayList as an implementation is required then use

    DoubleStream.of(doublesArray).boxed().collect(Collectors.toCollection(ArrayList::new));
    

    This one-liner doesn't require any additional libraries.

提交回复
热议问题