如何将值返回到2D数组?

时间:2018-04-17 21:28:24

标签: java arrays multidimensional-array compiler-errors return-value

我必须使用2D数组打印基于释放速度和角度的物体走多远的表格。下面是用于确定距离然后将其转换为英尺的方法。但是,我不能使用return语句将它分配到适当的位置,因为"不兼容的类型" error: converting a double [][] to double []。我如何将值返回到数组?

public double [] calcDistance()
{
    double [][] total ={ {0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0},
                         {0,0,0,0,0,0,0}  };

    for (int j = 0;  j < velocity.length; j++)
    {  
        for (int i = 0; i < degrees.length; i++)
      {
         total[j][i] = (Math.pow((velocity[j]*.447), 2) * Math.sin( 2 * Math.toRadians(degrees[i])) / 9.8);
         total[j][i] = total[j][i] * 3.28084;
      }
    }
    return total;

}

2 个答案:

答案 0 :(得分:2)

只需定义返回二维数组的方法:

public double[][] calcDistance() {
    // Rest of your code...

答案 1 :(得分:1)

您只能返回与方法类型相同的对象。 所以你只需要改变方法即可     public double [][] calcDistance()