When I reverse iterate over an ArrayList I am getting a IndexOutOfBoundsException. I tried doing forward iteration and there is no problem. I expect and know that there are five
Start the iteration at list.size() - 1
because array (or ArrayList
) elements are numbered from 0 up through 1 less than the size of the list. This is a fairly standard idiom:
for (int j = list.size() - 1; j >= 0; j--) {
// whatever
}
Note that your forward iteration works because it stops before reaching list.size()
.