IndexOutOfBoundsException with Android ArrayList

☆樱花仙子☆ 提交于 2019-12-02 18:39:47

问题


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?


回答1:


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++) {



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/10262269/indexoutofboundsexception-with-android-arraylist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!