2016-09-24 126 views
0

我想填充我与来自回路双值双数组,但我得到以下错误: 不兼容的类型:双重可能有损转换成int填充双值双阵列

,这里是我的代码:

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错误:不兼容的类型:可能有损从双转换为int

回答

2

你是试图给一个数组的0.000001地方分配一个值。那不会飞。数组/列表元素的位置是完整整数 - 0,1,2,3等。

A double数组意味着它是一个数组,其值为double类型,而不是这些值的单数可以加倍。

0

你不能有一个数组的非整数索引(所以像foo [0.25]没有意义)。你可能需要使用一个数据类型,它允许双精度(大写D作为对象)作为键(如迭代顺序很重要,就像LinkedHashMap)或类似的。或者,您可以使用整数作为索引,并将其划分为图的双X值(这将更节省空间)。你的来电。