ArrayList indexoutofbounds exception and sorting

I am getting an Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 146, Size: 146 and I am stumped, I've been trying to fix it for 4 days and I just don't get it. I am trying to sort all the arraylists based on averages using an insertion sort. I left out all the swapping of the other arrayLists as its rather pointless to waste space.

for(i=1;i<average.size()-1;i++){

boolean inserted =false;

j = i;

while((j>=1) && (inserted ==false)){

if(average.get(j)<average.get(j-1)){

//name

error -->nameTemp=name.get(j);

name.set(j,name.get(j-1));

name.set(j-1,nameTemp);

}else{

inserted =true;

}

j--;

}

}

[1249 byte] By [XanMasa] at [2007-11-26 22:04:27]
# 1
You're testing averages.size() but you're dependent on name.size() being at least this size. Is it? Also I don't think this will do any sorting, as it rearranges 'names' but compares 'averages'. I think you need to rethink this a little.
ejpa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...
# 2

The entire code is

for(i=1;i<average.size()-1;i++) {

boolean inserted = false;

j = i;

while((j>=1) && (inserted == false)) {

if(average.get(j)<average.get(j-1)) {

//name

nameTemp=name.get(j);

name.set(j,name.get(j-1));

name.set(j-1,nameTemp);

//grade 1

gradeTemp=grade1.get(j);

grade1.set(j,grade1.get(j-1));

grade1.set(j-1,gradeTemp);

//grade 2

gradeTemp=grade2.get(j);

grade2.set(j,grade2.get(j-1));

grade2.set(j-1,gradeTemp);

//grade 3

gradeTemp=grade3.get(j);

grade3.set(j,grade3.get(j-1));

grade3.set(j-1,gradeTemp);

//grade 4

gradeTemp=grade4.get(j);

grade4.set(j,grade4.get(j-1));

grade4.set(j-1,gradeTemp);

//average

averageTemp=average.get(j);

average.set(j,average.get(j-1));

average.set(j-1,averageTemp);

//letter grade

letterTemp = letter.get(j);

letter.set(j,letter.get(j-1));

letter.set(j-1,letterTemp);

//gender

genderTemp = gender.get(j);

gender.set(j,gender.get(j-1));

gender.set(j-1,genderTemp);

} else {

inserted = true;

}

j++;

}

}

>

XanMasa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...
# 3
OK bu tyou haven't addressed my question.
ejpa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...
# 4

All the arraylists are imported from a textfile given to me with the exception of average and letter which are defined later with the data given in the importation. Therefore, when I print out the averages in order, to also print out the grades 1-4, letter, etc I would also have to switch them in the same fashion as the average sort. I understand that I am rearranging it as that is my understanding of what a sort is, the conditional rearranging of an array to form a nice, organized array. I hope this is what it is doing. When I reduce it down to just sorting averages, forgetting every other arraylist, I still receive this same error.

My understanding is that an insertion sort works to move the smallest element up step by step and as it does this, it orders it. For example, 54321, 4 is brought to 0 to make 45321, 3 is brought to 1 which makes 43521, etc until we get 12345. Is this correct?

XanMasa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...
# 5
My question was about names.size(). Clearly names.size() < averages.size(), hence the exception.
ejpa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...
# 6

When

System.out.println("name size is" + name.size());

System.out.println("average size is" + average.size());

System.out.println("g1 size is" + grade1.size());

System.out.println("letter size is" + letter.size());

is used, it prints this:

name size is73

average size is73

g1 size is73

letter size is73

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 73, Size: 73

at java.util.ArrayList.RangeCheck(ArrayList.java:547)

at java.util.ArrayList.get(ArrayList.java:322)

at lab18tm1.GradesTM1a.sortTM(GradesTM1a.java:229)

at lab18tm1.Lab18TM1.menuSwitch(Lab18TM1.java:61)

at lab18tm1.Lab18TM1.main(Lab18TM1.java:47)

at lab18tm1.Lab18TM1.menuSwitch(Lab18TM1.java:59)

at lab18tm1.Lab18TM1.main(Lab18TM1.java:47)

As generated using JCreator 4.0 (forced by my school, I would prefer netBeans)

XanMasa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...
# 7
Well your code works for me without any exceptions, and with the obvious correction to the 'for' loop it even sorts correctly.I suggest recompilation.
ejpa at 2007-7-10 10:47:50 > top of Java-index,Core,Core APIs...