Greenhorn
Posts: 15
I need to be able to load an array with random integers. Here is what I have so far and I was wondering if someone could provide a little insight as to what I'm doing wrong or maybe point me a bit more in the right direction. The method I have currently gives me the error

java.lang.ArrayIndexOutOfBoundsException: 10

(where "10" is the arraySize declared in the constructor)

Thanks.

Matt Cartwright
Ranch Hand
Posts: 152

your iteration variable (i) starts counting at zero (0), the lenght of the array
obtained by numbers.length starts counting at one (1).

You get it?

Md Ibrahim
Greenhorn
Posts: 14
Hi Rene,

This problem is very silly....

Matt is perfect here.....

Just replace your old for statement with the below one it will work.

for (int i = 0; i <numbers.length ; i++){

If you still have any doubts or not getting output do post back.

By,
Ibrahim

Greenhorn
Posts: 15
I think I get it.

Does x.length ALWAYS start at 1? If so I guess I should never use '(or)equals to' but rather always less than.

Is this correct?

Fred Hamilton
Ranch Hand
Posts: 684
Rene Rad wrote:I think I get it.

Does x.length ALWAYS start at 1? If so I guess I should never use '(or)equals to' but rather always less than.

Is this correct?

the issue is that array indexes start at 0. if the array has length 10, then we have elements numbered 0,1,2,3,4,5,6,7,8,9.

Count em. There's 10 numbers there. An array with length = 1 has only one index, 0

Greenhorn
Posts: 15
Fred Hamilton wrote:
Rene Rad wrote:I think I get it.

Does x.length ALWAYS start at 1? If so I guess I should never use '(or)equals to' but rather always less than.

Is this correct?

the issue is that array indexes start at 0. if the array has length 10, then we have elements numbered 0,1,2,3,4,5,6,7,8,9.

Count em. There's 10 numbers there.

I understand that, 0 is still a number. does .length always start at 1?

Another question...

Also I found Random " Random ranNumVar = new Random();" on a site explaining the random class but I don't fully understand how its working. I copied it and adapted it to my code. Do you think you can try explaining to me what exactly is going on here?

I know that ranNumVar is an new variable that I declared in the line and its a type is Random. Is Random() the constructor in the Random class?

Fred Hamilton
Ranch Hand
Posts: 684
Fred Hamilton wrote:
Rene Rad wrote:I think I get it.

Does x.length ALWAYS start at 1? If so I guess I should never use '(or)equals to' but rather always less than.

Is this correct?

the issue is that array indexes start at 0. if the array has length 10, then we have elements numbered 0,1,2,3,4,5,6,7,8,9.

Count em. There's 10 numbers there.

I understand that, 0 is still a number. does .length always start at 1?

You certainly don't give the impression of understanding it. Whether or not length starts as 1 doesn't tel you whether or not to use < or <=. Anyways, good luck with it. Try to think a bit longer before you reply

Paul Clapham
Sheriff
Posts: 21117
32
In case you're still confused, the line of code posted by Md Ibrahim:

is the standard Java idiom for iterating through the elements of an array. So generally if you're doing a simple run through an array, this is how you should do it. There are other equivalent ways but Java programmers will (or at least should) always recognize this one.

Rob Spoor
Sheriff
Posts: 20546
57
Rene Rad wrote:I understand that, 0 is still a number. does .length always start at 1?

No, it starts at 0. The array is empty then. But the for-loop as suggested will still work: the guard (i < numbers.length) will fail immediately since 0 is not smaller than 0.

 Don't get me started about those stupid light bulbs.