java - 将数组与其自身相乘

标签 java arrays

我需要帮助将相同的数组与其自身相乘(对其进行平方)。我尝试复制它并创建一个新数组,但仍然收到类型不兼容的错误。该错误在 radius1 上突出显示,指出类型不兼容。

public static double[] part1(double[] z, double[] radius,double [] radius1)
{
    for(int index=0; index <10; index++)
    {
       z= radius[index]*radius1[index];
      }



    // The formula to calculate gravity is:
    // 6.67E-11 times the massOfPlanet divided by the radius of the planet squared

    return z;
}

     public static void main(String[] args)throws IOException
    {
    double[] radius = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double[] radius1 = {2439.7, 6051.9, 6378, 3402.5, 71492, 60270, 25562, 24774, 1195};
    double z[]= new double [9];
}

最佳答案

考虑一下

z= radius[index]*radius1[index];

z 被声明为 double[]。您正在使用表达式

访问数组 radiusradius1 中的组件
radius[index] * radius1[index];

其计算结果为double值。

您不能将 double 值分配给 double[] 类型的引用变量。

关于java - 将数组与其自身相乘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20484285/

相关文章:

java - 在 Actionscript 3 中从 Java Servlet 接收 ByteArray

java - 如何根据x调整 Actor z索引?

java - 如何让 Java 匹配 JavaScript 的 encodeURIComponent() 方法?

javascript - 如何过滤掉数组中重复的对象?

c++ - Makefile 只重新编译更改的对象?

c - 释放字符串数组的函数

java - 如何使用 Dagger 重写 Component 的 Builder 接口(interface)

java - 如何使用 hasNextInt() 捕获异常?我需要 Int 但如果输入是字符,那就不好了

java - 如何将属性 ;DB_CLOSE_ON_EXIT=FALSE 设置为 oracle db url?

javascript - 将嵌套值添加到关联数组 - JS