• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Loading an array with random integers.

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
Ranch Hand
Posts: 152
VI Editor Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

 
Marshal
Posts: 25144
64
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 21863
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
It's feeding time! Give me the food you were going to give to this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!