简体   繁体   English

从字符串返回双精度值数组

[英]Returning an array of double values from string

String numbers = ("5,25,77,44,64,8,55,28,86,35"); 字符串数字=(“ 5,25,77,44,64,8,55,28,86,35”);

I would like to return an array of double values from the string. 我想从字符串返回一个双精度值数组。 I am unable to do that successfully with the following code : 我无法使用以下代码成功完成此操作:

public static double[] fillArray(String numbers)
{
    String[] answers = numbers.split(",");

    double[] boom = new double[answers.length];

    for (int index = 0; index < answers.length; index++)
    {
        boom[index] = Double.parseDouble(answers[index]);
    }

    return boom;
}

The error I get is quite weird... 我得到的错误很奇怪...

 [De56f047c 

Why is that and how do I fix it? 为什么会这样,我该如何解决?

You're calling toString() on an array - and arrays don't override Object.toString() . 您在数组上调用toString() ,并且数组不会覆盖Object.toString() So you get the behaviour from Object.toString() : 因此,您可以从Object.toString()获得行为:

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号字符“ @”以及该对象的哈希码的无符号十六进制表示形式。 In other words, this method returns a string equal to the value of: 换句话说,此方法返回的字符串等于:

 getClass().getName() + '@' + Integer.toHexString(hashCode()) 

Call Arrays.toString(double[]) on the result instead and it will be fine. 而是对结果调用Arrays.toString(double[]) ,这样就可以了。 So this code: 所以这段代码:

double[] array = fillArray("5,25,77,44,64,8,55,28,86,35");
System.out.println(Arrays.toString(array));

... gives a result of: ...给出以下结果:

[5.0, 25.0, 77.0, 44.0, 64.0, 8.0, 55.0, 28.0, 86.0, 35.0]

I really wish toString() had been overridden for all arrays. 我真的希望toString()被所有数组覆盖。 It would make life simpler... 它将使生活更简单...

If I follow what you want, you can use this - 如果我遵循您的要求,则可以使用此功能-

public static void main(String[] args) {
    String numbers = ("5,25,77,44,64,8,55,28,86,35");
    List<Double> al = new ArrayList<Double>();
    for (String str : numbers.split(",")) {
        al.add(Double.valueOf(str));
    }
    double[] boom = new double[al.size()];
    for (int i = 0; i < al.size(); i++) {
        boom[i] = al.get(i);
    }
    System.out.println(Arrays.toString(boom));
}

Output is, 输出是

[5.0, 25.0, 77.0, 44.0, 64.0, 8.0, 55.0, 28.0, 86.0, 35.0]

You're not printing out the array result properly. 您没有正确打印出阵列结果。

double[] array = fillArray("5,25,77,44,64,8,55,28,86,35");

You probably did this: 您可能这样做:

System.out.println(array);

But you can't do that to print out an array. 但是您不能这样做来打印出一个数组。

The result you got is the 您得到的结果是

name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. 对象是实例的类的名称,符号字符“ @”以及对象的哈希码的无符号十六进制表示形式。

Look here to read up on that . 请看这里继续阅读

You need to iterate through the array, or use Arrays.toString(array[]) 您需要遍历数组,或使用Arrays.toString(array[])

So for example: 因此,例如:

for (double d : array) { 
    System.out.print(d + " ");
}

Or simply: 或者简单地:

System.out.println(Arrays.toString(array));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM