• Post Reply Bookmark Topic Watch Topic
  • New Topic

lottery numbers (array)  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am coming across an error in my code. I keep getting an "ArrayIndexOutOfBounds" error.
It prints, "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6". I understand it has something to do with the number of counters in the array because when I change the number, the error changes to the new one I put in. I'm really confused on how arrays work so any help would be greatly appreciated.


]
 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe Niksa,

Welcome to CodeRanch!

Joe Niksa wrote:when I change the number, the error changes to the new one I put in

That is because the problem is not in length of array, but your logic of iterating over the array:

I hope you are aware that in an array of length 6, maximum index you can access is 5. If you are not aware of how array works, please check out relevant sections in any data structure book of your choice.

I hope this helps.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arrays are zero indexed, which means the first element on an array is at index 0. This is because of how pointer arithmetic works. An array identifier (the name of the array) is essentially a memory address of where the array begins in memory. For an int array called foo[5], you have 5 elements on the array. The actual indices of the elements range from 0-4. When you try to get foo[0], what you are essentially saying is go to the memory address specified by foo + the datatype size (32 bits) * the index I specified (0) and retrieve the first range of bits of the datatype size (once again, 32 bits). Following this, if you were to access foo[2], you would be going to the memory address of foo + (32 * 2) and reading the first 32 bits from that location. I hope this clears up a little of how arrays work for you.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Hagberg wrote:This is because of how pointer arithmetic works.


Historically, yes, but not relevant for the Java language per se.

An array identifier (the name of the array) is essentially a memory address of where the array begins in memory. For an int array called foo[5], you have 5 elements on the array. The actual indices of the elements range from 0-4. When you try to get foo[0], what you are essentially saying is go to the memory address specified by foo + the datatype size (32 bits) * the index I specified (0) and retrieve the first range of bits of the datatype size (once again, 32 bits).


In C/C++, yes.

In any given JVM's implementation of a Java language array, probably, but not actually required by the spec as far as I know.

In the Java language, not at all, as there is no such thing as a memory address.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!