gpt4 book ai didi

java - 无法将 double 转换为 double [] 错误

转载 作者:行者123 更新时间:2023-12-02 04:48:45 25 4
gpt4 key购买 nike

对于一个项目,我必须将字符串数组拆分为单独的数字,然后通过将数字解析为 double 组来设置序列。

这是我到目前为止的代码:

import java.util.ArrayList;

public class Sequence
{
// the numbers in the sequence
private double[] sequence;

// sets up sequence by parsing s
public Sequence(String s)
{
String[] numbers = s.split(", ");
double[] storage = new double [numbers.length];
for (int x = 0; x < numbers.length; x = x+1) {
storage = Double.parseDouble(numbers[x]);
}
}

出于某种原因,当我尝试编译它时,我收到一条错误消息

不兼容的类型:double 无法转换为 double[]

我在网上寻找解决方案,但我是 Java 的初学者,真的不知道在这里该怎么做。

为什么我会收到此错误以及如何修复它,尤其是无需添加其他我不理解的方法?

提前致谢

最佳答案

storage[x] = Double.parseDouble(numbers[x]);

来自docs

public static double parseDouble(String s)

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

并且存储是一个数组,因此编译器会说不兼容的类型:double无法转换为double[]。相反,您可以通过指定数组索引来逐个存储每个元素存储[索引]

关于java - 无法将 double 转换为 double [] 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29443037/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com