简体   繁体   中英

IndexOutOfBoundsException with Android ArrayList

I've got a very annoying problem with some code throwing an IndexOutOfBoundsException and I really cannot understand why. The logcat points to the "addTimetableItem" of the following code which ill explain more on:

if(sortedFridayTimes.size()>0){
    insertDay("Friday");
    for(int i=1; i<sortedFridayTimes.size()+1;i++){
        addTimetableItem(sortedFridayTimes.get(i));
    }
}

"sortedFridayTimes" is an ArrayList containing my own "Timetable Entry" objects which I have sorted into order already. First the size is checked to see if there are any objects and if there is then "insertDay" runs which creates a new textview for a title and adds it to the layout (This works fine..). Inside the for loop the idea is to then add all the objects from the arraylist into the layout. Now I know that the "addTimetableItem" code works as ive tested it already, but my problem is that i cant seem to get the last object out of the arraylist. If I declare the for loop to only run for

"i<sortedFridayTimes.size()" 

then the program runs fine but I don't get the last entry in the arraylist which I know exists because I've debugged and watched my variables. On adding the "+1" as shown above I now get the IndexOutOfBoundsException and I really don't know why. As I've said, I've debugged and I know that an entry exists in the arraylist where I'm trying to point to, but it just crashes. I can provide more code if needs be, but does anyone have any ideas please?

You should accept @Tim's or @Graham's answer, this is just an addendum. They're correct about your size()+1 going past the end of the array.

If you're having difficulty using indexes to properly get everything out of the list, you can also try using a for-each loop (depending on the version of the Android SDK you're using). I'm assuming sortedFridayTimes is a list of class TimetableItem since you don't specify.

So this:

if(sortedFridayTimes.size()>0){
    insertDay("Friday");
    for(int i=1; i<sortedFridayTimes.size()+1;i++){
        addTimetableItem(sortedFridayTimes.get(i));
    }
}

Becomes this:

if(!sortedFridayTimes.isEmtpy()){
    insertDay("Friday");
    for(TimetimeItem item : sortedFridayTimes){
        addTimetableItem(item);
    }
}

A little cleaner if you don't actually need to use i anywhere.

i<sortedFridayTimes.size()+1

You are looping past the last element in the array. Why the +1 ?

If there are N elements in the array, then the elements are from indexes 0 through to N-1 .

So it should be:

for(int i=0; i<sortedFridayTimes.size(); i++) {

The last loop in your for loop runs:

sortedFridayTimes.get(sortedFridayTimes.size())

This will always be out of bounds, because the elements are zero indexed.

For example, if the array size is "5", then you cannot access index "5", because the 5 elements in the array are 0,1,2,3,4 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM