• 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
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

Compound assignment operator causes ArrayIndexOutOfBoundsException

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a for loop that's being used to populate an integer array:

Somehow, the loop has no problem populating the array with the i+5 (even 14, which is 4 past 10 integer elements), but when I use i += 5, I get this runtime error:

0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at Chapter3.Arrays.main(Arrays.java:12)

Why?
 
Saloon Keeper
Posts: 6417
60
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are incrementing 'i' in two places, the for() loop and i+=5. 'i' ends up being 11 which is beyond the end of your array.
 
Rancher
Posts: 4315
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the first (commented out) case you are not incrementing 'i', you are simply assigning the value of i+5 to the i'th entry in the array.

In the second (failing) case you are incrementing i by 5 and assigning that value to the pre-increment i'th entry.
Then, when you attempt to print out the value, i will eventually be beyond the size of the array and *bang*.
 
Sam Peterson
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the for loop declaration, why does "numbers.length" not need to be "numbers.length-1"?
 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are iterating the whole of the array, so you want array.length. Remember that the < operator will stop the loop before you get to array.length. If you have a 10‑element array, the loop will stop after index 9.
 
Marshal
Posts: 14339
237
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sam Peterson wrote:In the for loop declaration, why does "numbers.length" not need to be "numbers.length-1"?


This is a Java idiom you should get used to. Most index values in Java are 0-based so, as Campbell explained, if you have an array with 10 elements, the index values will range from 0 to 9.

The idiomatic way to iterate over an entire array in Java is this:

With the introduction of the enhanced for-loop in Java 5, if you are simply interested in iterating over the array and using the element values and don't care about the index, then you can do this:
 
After some pecan pie, you might want to cleanse your palatte with 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!