0

The following code runs until it throws an IndexOutOfBoundExceptionstating h is -2147483648.

At the beginning h=0and it only gets incremented, never decremented. How could it become negative?

float[] powerArray = new float[8760];
int h = 0;
for (TShortIterator it = heightData.iterator(); it.hasNext();) {
    short wspeed = it.next();
    if (h < 8760) {                 
        powerArray[h] = Math.min(wspeed, 4);
    }
    h++;
}
Stefan
  • 10,010
  • 7
  • 61
  • 117
  • 1
    This is kinda weird to iterate with a range based loop like that – Murat Karagöz Dec 06 '16 at 14:55
  • 1
    That's a poor quality question and answer. In the current state, the correct answer is just `because array index has to be between 0 and the array length, and -2147483648 is not` – jhamon Dec 06 '16 at 15:02
  • I did not write the original code but had to handle it. – Stefan Dec 06 '16 at 15:03
  • @Jhamon: Write it as answer and I'll accept it. :) The purpose of this question is to get a result when searching for indexoutoufboundexception and -2147483648. – Stefan Dec 06 '16 at 15:05

1 Answers1

1

The issue occurs for the case that the iterator has more entries than Integer.MAX_VALUE.

h is incremented until it reaches Integer.MAX_VALUE. On the next increment, the integer is overflown and becomes Integer.MIN_VALUE=-2147483648.

Then the expression powerArray[-2147483648] fails due to the negative index.

Stefan
  • 10,010
  • 7
  • 61
  • 117
  • 2
    Did you ask and answer at the same time? Why would you ask if you knew the answer? – Aidin Dec 06 '16 at 14:49
  • 2
    @Aidin There's nothing wrong with [answering your own question](http://stackoverflow.com/help/self-answer). – Jesper Dec 06 '16 at 14:51
  • 1
    @jesper Yes, I get that. But he does it literally the same minute that he asks – Aidin Dec 06 '16 at 14:52
  • @Aidin Stack allows this, if you have something non-duplicate and useful then you can answer it yourself but in this case , since there is no code about what `getHeightData(sensorHight)` does, so not quite sure about it – Pavneet_Singh Dec 06 '16 at 14:52
  • @Aidin Yes, so what? SO even has a special option to let you post the question and answer at the same time. – Jesper Dec 06 '16 at 14:53
  • Yes. It took me some time to solve the issue. Searching for IndexOutOfBoundException and iterator did not help.... so I thought the answer might help others facing similar issues. It's also a note for future me to not forget about it. :) – Stefan Dec 06 '16 at 14:55
  • @Jesper Ok, I wasn't aware of that. – Aidin Dec 06 '16 at 14:55
  • the answer was already [here](http://stackoverflow.com/questions/5131131/what-happens-when-you-increment-an-integer-beyond-its-max-value) and [there](http://stackoverflow.com/questions/3001836/how-does-java-handle-integer-underflows-and-overflows-and-how-would-you-check-fo) – jhamon Dec 06 '16 at 14:58
  • Same answer but different question... Once I knew that the int limit is the issue ... yes... then the solution was easy. – Stefan Dec 06 '16 at 15:01