将双精度数组转换为双精度 ArrayList

发布于 2024-10-20 05:41:18 字数 392 浏览 2 评论 0原文

当我尝试将双精度数组转换为双精度数组列表时,出现以下错误:

线程“main”中的异常 java.lang.ClassCastException: [D 无法转换为 java.lang.Double

下面是我的代码。

double [] firstValueArray ;

ArrayList; firstValueList = new ArrayList (Arrays.asList(firstValueArray));

我正在将此列表与另一个列表进行比较,并将结果分配给另一个双变量。

请让我知道这个错误的原因。

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 cannot be cast to java.lang.Double

Below is my code.

double [] firstValueArray ;

ArrayList <Double> firstValueList = new ArrayList (Arrays.asList(firstValueArray));

I am comparing this list with another list and assign the result to another double variable.

Please let me know the reason for this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

断肠人 2024-10-27 05:41:18

唉,Arrays.asList(..) 不适用于基元。 Apache commons-lang 有

Double[] doubleArray = ArrayUtils.toObject(durationValueArray);
List<Double> list = Arrays.asList(doubleArray);

Alas, Arrays.asList(..) doesn't work with primitives. Apache commons-lang has

Double[] doubleArray = ArrayUtils.toObject(durationValueArray);
List<Double> list = Arrays.asList(doubleArray);
稀香 2024-10-27 05:41:18

使用 Java 8 Streams API 可以实现这一点

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

。从 java 16 开始,这变得更加简洁:

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

如果需要返回 ArrayList 作为实现,则使用

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

此单行代码不需要任何其他库。

Using Java 8 Streams API this is achieved with

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

Since java 16 this became a bit more concise:

DoubleStream.of(doublesArray).boxed().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.

如此安好 2024-10-27 05:41:18

Guava 的版本更短:

List<Double> list = Doubles.asList(doubleArray);

参考:

  • < a href="http://guava-libraries.googlecode.com/svn/tags/release08/javadoc/com/google/common/primitives/Doubles.html#asList%28double...%29" rel="noreferrer" >Doubles.asList(double ...)

注意:这是一个可变参数方法。所有可变参数方法都可以使用相同类型的数组来调用(但不是相应的装箱/未装箱类型!!)。这两个调用是等效的:

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

此外,Guava 版本不执行完整的遍历,它是基元数组的实时列表视图,仅在访问基元时将基元转换为对象。

Guava's version is even shorter:

List<Double> list = Doubles.asList(doubleArray);

Reference:

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.

旧城烟雨 2024-10-27 05:41:18

感谢 bestsss 的评论,这应该是答案:

ArrayList<Double> firstValueList = new ArrayList<Double>();
for(double d : firstValueArray) firstValueList.add(d);

Credit to bestsss for the comment which should be the answer:

ArrayList<Double> firstValueList = new ArrayList<Double>();
for(double d : firstValueArray) firstValueList.add(d);
他夏了夏天 2024-10-27 05:41:18

…或者使用 Java 1.7:

double[] firstValueArray = new double[] {1.0, 2.0, 3.0};

ArrayList<Double> list = DoubleStream.of( firstValueArray ).boxed().collect(
    Collectors.toCollection( new Supplier<ArrayList<Double>>() {
      public ArrayList<Double> get() {
        return( new ArrayList<Double>() );
      }
    } ) );

…or with Java 1.7:

double[] firstValueArray = new double[] {1.0, 2.0, 3.0};

ArrayList<Double> list = DoubleStream.of( firstValueArray ).boxed().collect(
    Collectors.toCollection( new Supplier<ArrayList<Double>>() {
      public ArrayList<Double> get() {
        return( new ArrayList<Double>() );
      }
    } ) );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文