aspose file tools*
The moose likes Beginning Java and the fly likes Loading an array with random integers. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Loading an array with random integers. " Watch "Loading an array with random integers. " New topic
Author

Loading an array with random integers.

Rene Rad
Greenhorn

Joined: Feb 10, 2010
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
at Lab9b.loadArray(Lab9b.java:17)

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

Thanks.

Matt Cartwright
Ranch Hand

Joined: Aug 25, 2008
Posts: 149


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

Joined: Mar 24, 2010
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

Rene Rad
Greenhorn

Joined: Feb 10, 2010
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

Joined: May 13, 2009
Posts: 679
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
Rene Rad
Greenhorn

Joined: Feb 10, 2010
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

Joined: May 13, 2009
Posts: 679
Rene Rad wrote:
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
Bartender

Joined: Oct 14, 2005
Posts: 18656
    
    8

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

Joined: Oct 27, 2005
Posts: 19719
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Loading an array with random integers.