1 2 Previous Next 17 Replies Latest reply: Feb 10, 2009 10:34 PM by 843785

# ArrayIndexOutOfBoundsException - help debug ?

Can someone help me debug this? I keep getting the out of bounds exception and I've tried adding 1 to the length....
``````     public int[] AscendingSort(int[] Students)
{
int temp = 0;

for(int i = 0 ; i < Students.length ; i++)
{
if(Students[i] > Students[i + 1])
{
temp = Students[i] ;
Students[i] = Students[i + 1];
Students[i + 1] = temp;

}
}
return Students;
}``````
• ###### 1. Re: ArrayIndexOutOfBoundsException - help debug ?
Let's say the array has 3 elements, at indices 0, 1, and 2.

Your third trip through the loop will be
``if(Students[2] > Students[3])``
If you have 3 elements, you only do 2 comparisons, not 3. 0/1 and 1/2. Likewise, an array with N elements has N - 1 adjacent pairs, so N - 1 comparisons.
• ###### 2. Re: ArrayIndexOutOfBoundsException - help debug ?
rollarace wrote:
``````               for(int i = 0 ; i < Students.length ; i++)
{
if(Students[i] > Students[i + 1])``````
Look at those two lines. You loop as long as i is less than Students.length.

Let's assuming that Students.length is 5 (i.e. Students contains 5 elements).

Now in the last iteration i will be 4 (as you won't enter the loop if i is equal to Students.length).

Then you compare the element at position i (=4) with the element at position i+1 (=5).

But there is no position 5 in this array! If it has length 5, then valid indices are 0, 1, 2, 3 and 4!

Therefore you get an ArrayIndexOutOfBoundsException.

Also, please name your variables and parameters with a lower-case letter. Only Types (CamelCase) and static final constants (UPPER_CASE) start with an upper case in Java.

And in the future whenever you ask about an exception, post the full error message and stack trace.
• ###### 3. Re: ArrayIndexOutOfBoundsException - help debug ?
I understand what you're talking about for the positioning and that's why I had added 1 to the length...the exact error is:

``````Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
• ###### 4. Re: ArrayIndexOutOfBoundsException - help debug ?
rollarace wrote:
I understand what you're talking about for the positioning and that's why I had added 1 to the length...the exact error is:
Um, if you're already going past the end, how is it going to help to go even further past the end by adding one more? And if that's not what you meant, then please show clearly what you did mean.
• ###### 5. Re: ArrayIndexOutOfBoundsException - help debug ?
And if you mean that you made the array one bigger than the number of students, and just didn't populate the last element, that won't help, because a) the code you showed uses the length of the array, not the number of students, so it's still the exact same thing producing the exact same error and b) there's no need to have an extra element in the array. All you have to do is only compare the elements you have.
``for (int ix = 0; ix < arr.length; ix++)``
You do know that you're allowed to put any boolean expression, not just "ix < arr.length" for the middle piece, right? And you do know that you can compare ix to other things than arr.length, right?
• ###### 6. Re: ArrayIndexOutOfBoundsException - help debug ?
Set i initially to 1. Then compare i-1 against 1. GG

Mel
• ###### 7. Re: ArrayIndexOutOfBoundsException - help debug ?
Melanie_Green wrote:
Set i initially to 1. Then compare i-1 against 1. GG

Mel
That's another way to do it. I was hoping the OP could get there himself with a hint or two though.
• ###### 8. Re: ArrayIndexOutOfBoundsException - help debug ?

but on the other hand, i'm not getting the exception error anymore
``````     public int[] AscendingSort(int[] Students)
{
int temp = 0;
for(int i = 1 ; i > Students.length ; i++)
{
if(Students[i] < Students[i - 1])
{
temp = Students[i] ;
Students[i] = Students[i + 1];
Students[i + 1] = temp;
}

}``````
• ###### 9. Re: ArrayIndexOutOfBoundsException - help debug ?
Melanie_Green wrote:
Set i initially to 1. Then compare i-1 against 1. GG
Was a tip not a solution, i wanted to give you an idea of how you could approach your problem. Its not just about plugging in numbers, you need to understand what is happening, once you understand that, then you will see your obvious error.

e.g. do you understand what your doing wrong here when you set i initially to 1 and your working with i and i-1, not i and i+1 as listed below
``````temp = Students[i] ;
Students[i] = Students[i + 1];
Students[i + 1] = temp;``````
Mel
• ###### 10. Re: ArrayIndexOutOfBoundsException - help debug ?
rollarace wrote:
Then start with an array of only 3 elements. Put print statements in your code so you can see exactly what's happening, and if needed, work through it with pencil and paper. For problems like this, telling you how to fix your code won't help you. You'll have to gut through understanding how it works.
• ###### 11. Re: ArrayIndexOutOfBoundsException - help debug ?
try
``for(int i = 0 ; i < Students.length-1 ; i++)``
thanks
• ###### 12. Re: ArrayIndexOutOfBoundsException - help debug ?
Suppose your array is like this

3,2,1.

So
``for(int i = 1 ; i > Students.length ; i++)``
this line evaluates to
``for(int i = 1 ; i > 3 ; i++)``
which is true.So the control enters the loop.

Now the next line is
``if(Students[i] < Students[i - 1])``
which inturn evaluates to
``if(Students[1] < Students[0])``
i.e
``if(2<3)``
which again evaluates to true.So the control now enters the if block

Now the block of code that follows i.e
``````temp = Students[i] ;
Students[i] = Students[i + 1];
Students[i + 1] = temp;``````
evaluates to
``````temp = Students[1] ;
Students[1] = Students[2];
Students[2] = temp;``````
which inturn evaluates to
``````temp = 2;
Students[1] = 1;
Students[2] = 2;``````
So instead of swaping 3 and 2 (Students[0] and Students[1]) your code is swapping 2 and 1 (Students[1] and Students[2])

So what you need to do is to swap only those array variables that you are checking

Again if you are going for sorting then you probably would be needing another nested loop
which would look something like this
``````for(int i = 0 ; i <Students.length-1 ; i++)
{
for(int j = i+1;j < Students.length;j++)
{
if(Students[j] < Students) { temp=Sutdents[i];
Students[i] = Students[j];
Students[j] = temp;
}
}
}
Now I believe it would sort the array

Edited by: phoenix_frm_ashes on Feb 10, 2009 11:36 PM

Edited by: phoenix_frm_ashes on Feb 10, 2009 11:39 PM

Edited by: phoenix_frm_ashes on Feb 10, 2009 11:40 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ``````
• ###### 13. Re: ArrayIndexOutOfBoundsException - help debug ?
> {quote:title=phoenix_frm_ashes wrote:}{quote} So, in reference to your moniker, what was you're earlier moniker that got flamed so badly that you needed to rise again?
• ###### 14. Re: ArrayIndexOutOfBoundsException - help debug ?
Earlier it used to be only phoenix you know ...
Then after 500 years I actually flamed into ashes...
So after being reborn from ashes only I took this name.....
[:D]

cheers....................................................
1 2 Previous Next