2016-09-24 171 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值(這將更節省空間)。你的來電。