2016-04-02 83 views
1

我想要爲x = rcos(a)和y = rsin(a)生成半徑爲1的圓的x和y值,但是當我使用三下面的不同方法我只通過手動添加它們來獲得正確的值,但我想將它們添加到循環中。Java ArrayList在使用for循環時存儲不同的值

ArrayList<Double> cCoords = new ArrayList<Double>(); 
double pi = Math.PI; 
double pi_over_two = Math.PI/2.0; 
double pi_over_four = Math.PI/4.0; 
double pi_over_180 = Math.PI/180.0; 
double piVals = 0; 


cCoords.add(Math.cos(0)); 
cCoords.add(Math.sin(0)); 
cCoords.add(Long.valueOf(Math.round(Math.cos(pi_over_two))).doubleValue()); 
cCoords.add(Math.sin(pi_over_two)); 
cCoords.add(Math.sin(pi_over_two+pi_over_two+pi_over_two)); 

////////Below values are printed correctly 

    cCoords.add(Math.cos(pi_over_four)); 
    cCoords.add(Math.sin(pi_over_four)); 

    cCoords.add(Math.cos(pi_over_four+pi_over_four)); 
    cCoords.add((Math.sin(pi_over_four+pi_over_four))); 

    cCoords.add(Math.cos(pi_over_four+pi_over_four+pi_over_four)); 
    cCoords.add(Math.sin(pi_over_four+pi_over_four+pi_over_four)); 

    cCoords.add(Math.cos(pi_over_four +pi_over_four+pi_over_four+pi_over_four)); 
    cCoords.add(Math.sin(pi_over_four +pi_over_four+pi_over_four+pi_over_four)); 

    cCoords.add(Math.cos(pi_over_four+pi_over_four + pi_over_four+pi_over_four+pi_over_four)); 
    cCoords.add(Math.sin(pi_over_four+pi_over_four +pi_over_four+pi_over_four+pi_over_four)); 

    cCoords.add(Math.cos(pi_over_four+pi_over_four+pi_over_four +pi_over_four+pi_over_four+pi_over_four)); 
    cCoords.add(Math.sin(pi_over_four+pi_over_four+pi_over_four + pi_over_four+pi_over_four+pi_over_four)); 


//////////////Above values are printed correctly 


    for(int i = 0; i < 6; i++) { // Wrong values 

     cCoords.add(i, Math.cos(pi_over_four)); 
     cCoords.add(i+1, Math.sin(pi_over_four)); 

     pi_over_four+=pi_over_four; 

    } 
///// 


    for(int i = 0; i < 6; i++) { // x=1 coordinate gets repeated with different y values 

     cCoords.add(Math.cos(pi_over_four)); 
     cCoords.add(Math.sin(pi_over_four)); 

     pi_over_four+=pi_over_four; 

    } 

//// 

    for(int i = 0; i < cCoords.size()-1; i+=2) { 
     System.out.print("(" + cCoords.get(i) + ","); 
     System.out.println(cCoords.get(i+1) + ")\n"); 
    } 

回答

1

pi_over_four在變化每次循環,因此在現實中,代替由2^x,其中x爲i + 1 2的每個要乘以時間相乘。想象一下,在第一個循環中,pi_over_four等於pi/4,在第二個等於pi/2時,但是您將pi/2添加到pi/4而不是pi/4,從而使第三個循環具有相等的性能到pi。將新的pi_over_four存儲在其他變量中以解決此問題。

2

由於您在更改變量pi_over_four本身的值後在循環中插入了錯誤的值,所以得到的值不正確。

例如,你說的是正確打印的值下面的代碼片段,你從剛剛pi_over_four一倍爲下一次迭代即2 * pi_over_four值:

cCoords.add(Math.cos(pi_over_four)); 
cCoords.add(Math.sin(pi_over_four)); 

cCoords.add(Math.cos(pi_over_four + pi_over_four)); 
cCoords.add(Math.sin(pi_over_four + pi_over_four)); 

在下一次迭代中變得3 * pi_over_four

cCoords.add(Math.cos(pi_over_four + pi_over_four + pi_over_four)); 
cCoords.add(Math.sin(pi_over_four + pi_over_four + pi_over_four)); 

請注意,在這裏您不會更改變量pi_over_four的值。你只是在做n * pi_over_four然後把它存回列表中。

但是,如果您仔細看到您的循環,則您正在更改pi_over_four本身的值,這是錯誤的。你應該做的卻是這樣的:

for(int i = 0; i < 6; i++) { 
    cCoords.add(i, Math.cos((i + 1) * pi_over_four)); 
    cCoords.add(i+1, Math.sin((i + 1) * pi_over_four)); 
} 
+0

謝謝大家的幫助下,找到了循環應該是類似 您的建議: INT J = 0; for(int i = 0; i <6; i + = 2)cCoords.add(i,Math.cos((j)* pi_over_four)); cCoords.add(i + 1,Math.sin((j)* pi_over_four)); j + = 0.5) – user1420482

+0

@ user1420482您的歡迎! :) – user2004685