• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Compound assignment operator causes ArrayIndexOutOfBoundsException

 
Ranch Hand
Posts: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 86
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the for loop declaration, why does "numbers.length" not need to be "numbers.length-1"?
 
Marshal
Posts: 76885
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17357
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
I don't like that guy. The tiny ad agrees with me.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic