([D @ 7852e922)尝试打印双精度数组时输出错误

时间:2018-11-28 17:00:01

标签: java arrays string

在尝试在方法中返回数组时,我一直出现类似[D @ 7852e922]的错误。请看我的代码。 (赋值:通过将所有当前条目替换为a的值并将其替换为b的值来更新Array。)(仅更改方法,而不更改标头)

 public static void main(String[] args){
 double[] num = new double[]{1,2,4,9,-2,1.2};
 System.out.println(UpdateEntry(num,2,3));}
 public static double[] UpdateEntry (double[] array, int a, int b)
 {
 double[] newArray = new double[array.length];
 for (int j = 0; j < newArray.length; j++){
 if (array[j] == a){newArray[j] = b;}
 else {newArray[j] = array[j];}
 }
 return newArray;
 }

输出:

  

[D @ 7852e922

1 个答案:

答案 0 :(得分:-1)

只需替换

System.out.println(UpdateEntry(num,2,3));

System.out.println(Arrays.toString(UpdateEntry(num,2,3));

此处是文档

https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#toString(double[])


这是一种手动操作的方式(摘自官方的jdk6)

public static String toString(double[] a) {

    if (a == null)

        return "null";

    int iMax = a.length - 1;

    if (iMax == -1)

        return "[]";


    StringBuilder b = new StringBuilder();

    b.append('[');

    for (int i = 0; ; i++) {

        b.append(a[i]);

        if (i == iMax)

            return b.append(']').toString();

        b.append(", ");

    }

}