简体   繁体   English

java.lang.IndexOutOfBoundsException:无效的索引5,大小为5

[英]java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5

This error occurs sometimes and sometimes it works properly. 有时会出现此错误,有时会正常工作。 I really can't understand what the problem is. 我真的不明白是什么问题。

int length = CitiesInfos.citiesOnTheRoad.length;
for (int i = 0; i < length; i++)
{
    HashMap<String,String> temp=new HashMap<String, String>();
    temp.put(FIRST_COLUMN, CitiesInfos.citiesOnTheRoad[i]);

    if (CitiesInfos.roadWorkArrayList.get(i)!=null)
    {
        temp.put(SECOND_COLUMN, CitiesInfos.weatherArrayList.get(i).getCelcius() + ", " +
                 CitiesInfos.weatherArrayList.get(i).getWeatherStatus());
    }
    else
    {
        temp.put(SECOND_COLUMN, " ");
    }
}

This is the line to which logCat directs. 这是logCat指向的行。 What can the problem be? 可能是什么问题?

increment variable i loops from 0 to citiesOnTheRoad.length - 1 , however, you also use i as an index to two other ArrayList variables: weatherArrayList and roadWorkArrayList , how are you certain they are the correct size? 增量变量i从0循环到citiesOnTheRoad.length - 1 ,但是,您也将i用作另外两个ArrayList变量的索引: weatherArrayListroadWorkArrayList ,如何确定它们的大小正确?

I added a check that will prevent the crash you are receiving, but I caution you to take a closer look at your code instead of putting this band-aid on it. 我添加了一张支票,可以防止您收到崩溃消息,但是我提醒您仔细检查代码,而不要在上面放一下此创可贴。

    int length = CitiesInfos.citiesOnTheRoad.length;
    for (int i = 0; i < length; i++)
    {
        HashMap<String,String> temp=new HashMap<String, String>();
        temp.put(FIRST_COLUMN, CitiesInfos.citiesOnTheRoad[i]);
        if(i < CitiesInfos.roadWorkArrayList.size() 
            && i < CitiesInfos.weatherArrayList.size() 
            && CitiesInfos.roadWorkArrayList.get(i)!=null)
        {
            temp.put(SECOND_COLUMN, CitiesInfos.weatherArrayList.get(i).getCelcius() + ", " + CitiesInfos.weatherArrayList.get(i).getWeatherStatus());
        }
        else
        {
            temp.put(SECOND_COLUMN, " ");
        }
}

As James Wierzba has mentioned, you are using the same control variable for the array lists in the loop and that is your problem. 正如James Wierzba所提到的,您对循环中的数组列表使用了相同的控制变量,这就是您的问题。

You need to ensure that there are enough items before you try to retrieve an element. 在尝试检索元素之前,需要确保有足够的项目。

A few suggestions: 一些建议:

it seems you are storing related data in two arrays, you are better of grouping this data together inside a class eg City. 似乎您将相关数据存储在两个数组中,最好将这些数据分组到一个类中,例如City。

You could then have the cities on the road array contain a number of city objects each storing information about the weather in that city. 然后,您可以使道路阵列上的城市包含多个城市对象,每个对象存储有关该城市天气的信息。

I would also suggest that you do not have your arrays as public variables, you should have them as private or protected and use getters/setters to access them. 我还建议您不要将数组作为公共变量,而应该将它们设置为私有或受保护,并使用getter / setter来访问它们。

暂无
暂无

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

相关问题 java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 Protobuf + java java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - Protobuf + java java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 java.lang.IndexOutOfBoundsException 问题:索引 0 无效,大小为 0 - issue with java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 Android java.lang.IndexOutOfBoundsException:无效的索引 0,大小为 0 - Android java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 java.lang.IndexOutOfBoundsException:无效的索引x,大小为x - java.lang.IndexOutOfBoundsException:Invalid index x, size is x java.lang.IndexOutOfBoundsException:无效的索引1,在Sectioned RecyclerView中大小为1 - java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 in Sectioned RecyclerView recyclerview onclicklistener java.lang.IndexOutOfBoundsException:无效的索引0,大小为0 - recyclerview onclicklistener java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 java.lang.IndexOutOfBoundsException:无效的索引20,大小为20 - java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20 java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0, TensorFlow on Android - java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0, TensorFlow on Android RecyclerView - java.lang.IndexOutOfBoundsException:索引 1 无效,大小为 0 - RecyclerView - java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM