2011-11-22 171 views
-2

我試圖將數組中的值設置爲變量。這裏是我的代碼:如何向數組添加變量

//init the array as a float 
//I have tried to put a value in the brackets, but it returns a different error. 
//I initialized it this way so I could call it from other methods 
private float[] map; 

// generate a "seed" for the array between 0 and 255 
float x = generator.nextInt(256); 
int n = 1; 
// insert into the first 25 slots 
while(n <= 25) { 
    // here's my problem with this next line 
    map[n] = x; 
    double y = generator.nextGaussian(); 
    x = (float)Math.ceil(y); 
    n = n + 1; 
} 

我打上我的錯誤行,返回的錯誤是:「在拋出未捕獲的異常......」。我究竟做錯了什麼???提前致謝。

編輯-----

這裏是整個異常:

Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main] 

我使用y以生成隨機高斯,則X轉換成float值,改變成浮動值

我很確定這是這條線,因爲這是我的編譯器告訴我的。

+4

你能發佈更多的異常。你還可以展示如何定義地圖? – Gray

+0

什麼是'map'? 'y'是什麼? – juliomalegria

+1

「地圖」的類型是什麼?錯誤的全部信息是什麼?可能不是下一行? (generator.nextGaussian();) – DPM

回答

6

我猜你會得到兩個例外之一:

  1. 你得到一個NullPointerException因爲已經初始化地圖null。指定例如使用非空值:

    private float[] map = new float[25]; 
    
  2. 由於你使用的是基於1的索引,而不是從零開始的索引得到一個IndexOutOfBoundsException

更改此:

int n = 1; 
while(n <= 25) { 
    // etc.. 
    n = n + 1; 
} 

對此for循環:

for (int n = 0; n < 25; ++n) { 
    // etc.. 
} 
+0

哦謝謝,我會試試看,謝謝你的快速回復 – JAW1025

+0

@ JAW1025:那麼......你打算告訴我們,你得到的例外是什麼類型?我的回答只是我最好的猜測。您發佈的信息太少,我無法確定。 –

+0

我更新了問題 – JAW1025