我试图用循环中的双精度值填充双精度数组,但得到以下错误:不兼容的类型:可能存在从双精度到整型的有损转换
下面是我的代码:
public class ThreadGraph extends Thread {
//initializing the variables
public static double x1=0.0; //left endpoint of interval
public double x2=1.0; //right endpoint of interval
public double y=4/(1+Math.pow(x1, 2)); //Deriving the two parallel trapezium lengths
public int n=1000000; //The Number of trapezia, 1000000 gives greater accuracy for deriving pi
public double h=(x2-x1)/n; //Each trapezium's constant height
public double trapeziaAreasSum=0; //This variable will store the sum of the areas of the trapezia
static double[]valuesOfx1=new double[1000000];
static double[]valuesOfy=new double[1000000];
//this method divides the area under the curve into trapezia
public void run(){
for(double x1=0.0; x1<1.0; x1=x1+0.000001, y=4/(1+Math.pow(x1,2))){
valuesOfx1[x1] = x1; //ERROR OCCURS HERE
valuesOfy[y] = y; //ERROR OCCURS HERE
}}}可能的问题是什么?因为循环的输出是双精度的,并且我试图将这些结果添加到双精度数组中,但是我得到了一个int错误:不兼容的类型:从双精度到整型的可能有损转换
发布于 2016-09-25 07:28:09
您正在尝试为数组的0.000001位赋值。那不会飞的。数组/列表元素的位置是全整数- 0、1、2、3等。
double数组意味着它是一个值为double类型的数组,而不是这些值的指数可以是双精度。
发布于 2016-09-25 07:31:37
你不能有一个非整数的数组索引(所以像foo0.25这样没有意义)。您需要使用允许双精度(对象为大写D)作为键(如果迭代顺序很重要,则类似于LinkedHashMap )的数据类型或类似的数据类型。或者使用int作为索引,并除以图形的双X值(这将更加节省空间)。你说了算。
https://stackoverflow.com/questions/39681891
复制相似问题