This content has been marked as final.
Show 17 replies

1. Re: ArrayIndexOutOfBoundsException  help debug ?
3004 Feb 10, 2009 6:58 PM (in response to 843785)Let's say the array has 3 elements, at indices 0, 1, and 2.
Your third trip through the loop will be
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.if(Students[2] > Students[3])

2. Re: ArrayIndexOutOfBoundsException  help debug ?
843785 Feb 10, 2009 7:01 PM (in response to 843785)rollarace wrote:
Look at those two lines. You loop as long as i is less than Students.length.for(int i = 0 ; i < Students.length ; i++) { if(Students[i] > Students[i + 1])
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 lowercase 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 ?
843785 Feb 10, 2009 7:14 PM (in response to 843785)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 at Grades.AscendingSort(StudentGrades.java:139) at StudentGrades.main(StudentGrades.java:69)

4. Re: ArrayIndexOutOfBoundsException  help debug ?
3004 Feb 10, 2009 7:16 PM (in response to 843785)rollarace wrote:
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.
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: 
5. Re: ArrayIndexOutOfBoundsException  help debug ?
3004 Feb 10, 2009 7:19 PM (in response to 3004)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.
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?for (int ix = 0; ix < arr.length; ix++)

6. Re: ArrayIndexOutOfBoundsException  help debug ?
699554 Feb 10, 2009 7:23 PM (in response to 843785)Set i initially to 1. Then compare i1 against 1. GG
Mel 
7. Re: ArrayIndexOutOfBoundsException  help debug ?
3004 Feb 10, 2009 7:24 PM (in response to 699554)Melanie_Green wrote:
That's another way to do it. I was hoping the OP could get there himself with a hint or two though.
Set i initially to 1. Then compare i1 against 1. GG
Mel 
8. Re: ArrayIndexOutOfBoundsException  help debug ?
843785 Feb 10, 2009 7:35 PM (in response to 3004)I get way too stressed about this stuff. for some reason it's still not sorting them...
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 ?
699554 Feb 10, 2009 8:02 PM (in response to 699554)Melanie_Green wrote:
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.
Set i initially to 1. Then compare i1 against 1. GG
e.g. do you understand what your doing wrong here when you set i initially to 1 and your working with i and i1, not i and i+1 as listed below
Meltemp = Students[i] ; Students[i] = Students[i + 1]; Students[i + 1] = temp;

10. Re: ArrayIndexOutOfBoundsException  help debug ?
3004 Feb 10, 2009 8:08 PM (in response to 843785)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.
I get way too stressed about this stuff. for some reason it's still not sorting them... 
11. Re: ArrayIndexOutOfBoundsException  help debug ?
843785 Feb 10, 2009 10:34 PM (in response to 843785)try
in the your code.for(int i = 0 ; i < Students.length1 ; i++)
thanks 
12. Re: ArrayIndexOutOfBoundsException  help debug ?
843785 Feb 11, 2009 1:41 AM (in response to 843785)Suppose your array is like this
3,2,1.
So
this line evaluates tofor(int i = 1 ; i > Students.length ; i++)
which is true.So the control enters the loop.for(int i = 1 ; i > 3 ; i++)
Now the next line is
which inturn evaluates toif(Students[i] < Students[i  1])
i.eif(Students[1] < Students[0])
which again evaluates to true.So the control now enters the if blockif(2<3)
Now the block of code that follows i.e
evaluates totemp = Students[i] ; Students[i] = Students[i + 1]; Students[i + 1] = temp;
which inturn evaluates totemp = Students[1] ; Students[1] = Students[2]; Students[2] = temp;
So instead of swaping 3 and 2 (Students[0] and Students[1]) your code is swapping 2 and 1 (Students[1] and Students[2])temp = 2; Students[1] = 1; Students[2] = 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 thisfor(int i = 0 ; i <Students.length1 ; 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 ?
masijade Feb 11, 2009 1:43 AM (in response to 843785)> {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 ?
843785 Feb 11, 2009 1:53 AM (in response to masijade)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....................................................