Convert a double array to Double ArrayList

前端 未结 5 1850
逝去的感伤
逝去的感伤 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:48

    Guava's version is even shorter:

    List list = Doubles.asList(doubleArray);
    

    Reference:

    • Doubles.asList(double ...)

    Note: This is a varargs method. All varargs methods can be called using an array of the same type (but not of the corresponding boxed / unboxed type!!). These two calls are equivalent:

    Doubles.asList(new double[]{1d,2d});
    Doubles.asList(1d,2d);
    

    Also, the Guava version doesn't do a full traverse, it's a live List view of the primitive array, converting primitives to Objects only when they are accessed.

提交回复
热议问题