错误[D @ 6521f956尝试打印数组时

时间:2015-11-11 21:13:27

标签: java arrays

我试图打印从方法返回的数组,但是我收到错误[D @ 6521f956在终端中。正如你从下面的代码中看到的那样,我循环遍历我的数组以计算体重指数,然后我再次放入一个新数组。我试图返回到我的主方法并在终端中打印这个数组,到目前为止没有运气。我有什么明显的错误吗?

public class Method {

    double [] bmiCalculation (double [] height, int [] weight){
    double [] bmiArray = new double[height.length];

    for (int i = 0; i < height.length && i < weight.length; i++) {

      double bmi = weight[i] / (height[i] * height[i]);
      bmiArray[i] = bmi;
    }

    return bmiArray;
  }
}

class Methodmain {

  public static void main (String [] args) {

    Method method = new Method();
    double [] heightArray = {1.78, 1.67, 1.59, 1.80, 1.90};
    int [] weightArray = {50, 60, 70, 80, 90};

    System.out.println(method.bmiCalculation(heightArray, weightArray));
  }
}

1 个答案:

答案 0 :(得分:1)

这不是错误 - 这正是您打印double[]时得到的结果,因为Java中的数组不会覆盖toString()

只需使用Arrays.toString(double[])代替:

double[] result = method.bmiCalculation(heightArray, weightArray);
System.out.println(Arrays.toString(result));