I have the following code in some app:
int lowRange=50;
int[] ageRangeIndividual = {6, 10, 18, 25, 45, 65, 90};
int index=0;
for (; index<ageRangeIndividual.length-1 && ageRangeIndividual[index]<=lowRange;index++);
I am getting an “Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 7″ in the for line! even though I explicitly specify to break the cycle if index < last indexable item in the array!
This does not happen always, but after some time of running said program (lowRange varies each time the function is called)
What am I not seeing?
The code you’ve posted doesn’t throw an exception, on its own. In fact, your array bounds check is one off – you’re being too conservative, unless you really don’t intend to check the last value. It would normally just be
You say it happens eventually – do you have any other threads changing the value of
indexorageRangeIndividual?I presume you’re about to use
indexin the next line – personally I’d find it clearer if this were wrapped up in a method:I really don’t like for loops which have no body, and which use an existing variable for indexing. It’s all perfectly valid, but it just feels wrong.