• Post Reply Bookmark Topic Watch Topic
  • New Topic

Histogram array out of bounds ?  RSS feed

 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea why I am I getting prompt "ArrayIndexOutOfBounds" ?? Maybe is something benign, but I just dont see it. The console prompt is also telling my to look at line number 16, but again I think that my logic is ok. Can someone tell me where is the problem ?

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The arrays out of bounds exception also tells you the index that caused the issue (in addition, to location). Perhaps that would be a good starting point? Which index are you trying to dereference?

Henry
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valid indices for an array with 100 elements are 0 upto and including 99.

In lines 9 to 11 you are generating random indices, but you are generating numbers between 0 upto and including 100 there, because you used 101 as the upper limit of the range in line 10.

Note that this might generate the value 100, which is not a valid index in the array arr.

To make it work, change the "101" in line 10 to "100".
 
Miljan Puletic
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thnaks, I got it now.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!