Quote:
quote:Originally posted by DRAYKKO
I'm getting an ArrayIndexOutOfBounds exception (at runtime) for the following piece of code:
for(int x = 0; x < dragonTextArray.length; x++)
{
if(dragonTextArray[x].compareTo(dragonTextArray[x+1]) < 0)
{
storage = dragonTextArray[x];
dragonTextArray[x] = dragonTextArray[x+1];
dragonTextArray[x+1] = storage;
According to the diagnostics the offending code is:
dragonTextArray[x] = dragonTextArray[x+1];
and I'm not sure how to fix this. I thought the statement "x < dragonArray.length" should take care of the loop extending beyond the last element in the array. Can someone give me some insight as to what's happening here?
|
Ah, a question about a dragon text!! (first in months)
The ArrayIndexOutOfBoundsException is always thrown when you try to address an index of an array that does not exist.
So the question is: where exactly is that bug?
It IS in the for statement, but for a specific reason. Normally you would not call array[x+1], but in the for block you do, so I suggest:
for(int x = 0; x < dragonTextArray.length-1; x++)
alright?
:0)
Francis @ ch15
P.S. Bubble sorting, eh? Looks good. Only.. you are sorting the array from Z to A..
Also: BEWARE! The bubble sort isn't finished after going through the array elements once. You have to start over again until ALL are in place (all comparisons are < 0 in case you want to store your dragon names backwards).