简体   繁体   English

Android ArrayList的IndexOutOfBoundsException

[英]IndexOutOfBoundsException with Android ArrayList

I've got a very annoying problem with some code throwing an IndexOutOfBoundsException and I really cannot understand why. 我有一些代码引发IndexOutOfBoundsException异常烦人的问题,我真的不明白为什么。 The logcat points to the "addTimetableItem" of the following code which ill explain more on: logcat指向以下代码的“ addTimetableItem”,这将无法进一步说明:

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. “ sortedFridayTimes”是一个包含我自己的“时间表条目”对象的ArrayList,我已经对其进行了排序。 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..). 首先检查大小以查看是否有任何对象,然后检查是否存在“ insertDay”,这将为标题创建一个新的textview并将其添加到布局中(这很好..)。 Inside the for loop the idea is to then add all the objects from the arraylist into the layout. 在for循环内,想法是将arraylist中的所有对象添加到布局中。 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. 现在我知道“ addTimetableItem”代码已经通过ive测试,但是我的问题是我似乎无法从arraylist中获取最后一个对象。 If I declare the for loop to only run for 如果我声明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. 然后程序可以正常运行,但我无法知道arraylist中的最后一个条目,因为我已经调试并监视了变量,所以知道它存在。 On adding the "+1" as shown above I now get the IndexOutOfBoundsException and I really don't know why. 如上所示,在添加“ +1”后,我现在得到了IndexOutOfBoundsException,但我真的不知道为什么。 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. 就像我说的那样,我已经调试了,而且我知道要尝试指向的arraylist中存在一个条目,但是它崩溃了。 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. 您应该接受@Tim或@Graham的答案,这只是一个附录。 They're correct about your size()+1 going past the end of the array. 他们对您的size()+1超出数组末尾是正确的。

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). 如果您在使用索引正确地将所有内容都排除在列表之外时遇到困难,也可以尝试使用for-each循环(取决于您使用的Android SDK的版本)。 I'm assuming sortedFridayTimes is a list of class TimetableItem since you don't specify. 我假设sortedFridayTimesTimetableItem类的列表,因为您未指定。

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可以使用一种更清洁的工具。

i<sortedFridayTimes.size()+1

You are looping past the last element in the array. 您正在循环经过数组中的最后一个元素。 Why the +1 ? 为什么+1

If there are N elements in the array, then the elements are from indexes 0 through to N-1 . 如果数组中有N元素,则元素从索引0到索引N-1

So it should be: 因此应该是:

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

The last loop in your for loop runs: for循环中的最后一个循环运行:

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 . 例如,如果数组大小为“ 5”,则不能访问索引“ 5”,因为数组中的5个元素是0,1,2,3,4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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