2011-11-22 68 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