2013-12-10 40 views
1

我需要帮助将相同的数组与自身相乘(将其平方)。我试图复制它,并创建一个新的数组,但我仍然遇到不兼容类型的错误。该错误突出显示了不同类型的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]; 
} 

回答

1

考虑这个

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

z被声明为double[]。您使用的表达

radius[index] * radius1[index]; 

此计算结果为double值访问该阵列radiusradius1中的组件。

您不能将double值分配给类型为double[]的参考变量。

0

我想你的意思

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

z是一个数组,而不是一个double

0

如果您只想对其进行平方处理,则不需要复制数组,只需将每个条目与自身相乘并将其存储在您阅读的位置即可。

public static double[] squareArray(double[] arr) { 
    for (int i=0; i < arr.length; i++) { 
    arr[i] = arr[i] * arr[i]; 
    } 
    return arr; 
} 

此外,您使用index < 10作为条件,但它最好有它的动态通过读取输入数组arr的长度(元素的数量)。

0

由于您试图将变量z(这是一个数组)声明为乘以两个双精度(也是double)的乘积,因此您会收到错误。如果您希望z中的每个INDEX都具有两个元素的乘积,请将z [index]而不是z设置为该产品。

0

Z是一个数组的radius[index]*radius1[index]结果是双

变化

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

你不需要两个数组,以举办“半径”值。你可以将它们保存在一个数组中。

此外,z是一个数组,而不是double。

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

试试这个

public static double[] part1(double[] radius,double [] radius1) 
{ 

    double[] z = new double[radius.length]; 
    for(int index=0; index <10; index++) 
    { 
     z[index]= radius[index]*radius1[index]; 
    } 

    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[]= par1(radius, radius1); 
} 
0

你需要做的是改变了Z型的for循环

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

你忘了,你已经被声明为数组

0

你应该在方法part1内部创建一个数组并返回它。此外,最好使用radius.length作为循环终止条件。在你的情况下,因为radius的长度是9,所以10是不正确的。

import java.util.Arrays; 

public class ArrayTest { 
    public static double[] part1(double[] radius, double[] radius1) { 
     double[] z = new double[radius.length]; 
     for (int index = 0; index < radius.length; index++) { 
      z[index] = 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) { 
     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[] = part1(radius, radius1); 
     System.out.println(Arrays.toString(z)); 
    } 
} 
0

您正试图设置z,这是一个double[],以两个double S中的产物,其计算结果为double。 Java中不允许将数组设置为单个值。

此外,即使您的阵列仅包含9个值,您的for循环10次,这将导致ArrayIndexOutOfBoundsException