Android java.lang.IndexOutOfBoundsException:索引2无效,大小为2

Android java.lang.IndexOutOfBoundsException:索引2无效,大小为2,android,arraylist,Android,Arraylist,情景:- 我有两个ArrayList 列表包含图像 postList包含所选图像的位置 现在,每当我选择图像并按delete menu(删除菜单)时,它都会删除所选图像。 当我在debug中运行代码时,它工作正常并给出所需的输出 但是,当我在正常模式下运行它时,它会崩溃并给出上面的异常 if (posList.size() > 0) { Toast.makeText(getBaseContext(), "i value" +posList.size(),

情景:-

我有两个
ArrayList

列表包含图像

postList包含所选图像的位置

现在,每当我选择图像并按delete menu(删除菜单)时,它都会删除所选图像。

当我在
debug
中运行代码时,它工作正常并给出所需的输出

但是,当我在正常模式下运行它时,它会崩溃并给出上面的
异常

if (posList.size() > 0)
{
    Toast.makeText(getBaseContext(), "i value" +posList.size(), 
                   Toast.LENGTH_SHORT).show();
    for (int i = 0; i < posList.size(); i++)
        appAdp.list.remove(appAdp.list.get(posList.get(i)));
    appAdp.notifyDataSetChanged();
    posList.clear();
    Toast.makeText(getBaseContext(), "You deleted selected items",
                   Toast.LENGTH_SHORT).show();              
}
return true;
此处显示错误

appAdp.list.remove(appAdp.list.get(posList.get(i)));
日志:-

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2

at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)

at java.util.ArrayList.get(ArrayList.java:304)
为什么它会这样,一点线索也没有


谢谢您的帮助。

记住索引是从零开始的。我想当你得到位置时,它比数组的索引高+1,所以你得到越界异常,错误是
无效索引2,大小是2

可能的问题是您的
posList.size()=2
其中as
appAdp.list.size()0)
i=appAdp.list.size();

当(i您试图在同一ArrayList上执行操作时,因为当您从ArrayList中删除elemnt时,它的大小将减小,因此,您将获得
ArrayIndexoutofBoundsException
。 i、 e当您从
appAdp.list
中删除项目时,
appAdp.list
的大小也会改变

考虑一下您的列表是否最初有3个元素

您的位置在posList 0,2中

然后,当您从
appAdp.list
0
项目中删除项目时,其大小将变为
2
,下次您尝试在
2
位置删除项目时,您将获得
AIOBE

解决方案:

将需要删除的所有项目保存在单独的列表中,并使用
removeAll(List)
方法从
appAdp.List

例如:

ArrayList<yourappListtype> templist=new ArrayList<yourappListtype>();
for (int i = 0; i < posList.size(); i++)
        templist.add(appAdp.list.get(posList.get(i)));

在更改适配器的数据源之前,可以调用适配器的
notifyDataSetValidated()
函数使原始数据源无效,然后调用适配器的
notifyDataSetChanged()
完成更改数据源后。

异常的基本含义java.lang.IndexOutOfBoundsException:索引x无效,大小为y,其中x和y分别是索引位置,大小表示您试图在不存在的位置获取值。例如,带有siz的ArrayListE2,通常您希望通过指定位置索引来获取值,在这种情况下,有效位置将为0或1,并最终指定位置2(索引2不存在)。我相信理解Java错误的基本含义将为您的开发节省大量宝贵的时间。

试试这段代码

postList.删除(位置);

那么


notifyItemRangeChanged(position,postList.size());

我有6个图像,我可以选择其中的任何一个,它返回的是相同的,如果我选择5,它应该将第5个位置放在0索引中。但是为什么异常“另外,if(posList.size()>0)从1检查而不是0。因此if条件应该是if(posList.size()>=0).但这不是问题,这是逻辑问题”“这是错的。”“是的,我forgot@CobraAjgar补充solution@CobraAjgar试着理解答案…请检查我提供的代码片段我的解决方案使用了for循环,但将I设置为等于ArrayList的大小减1,然后向后迭代并删除元素。不确定这是否是一个好的做法,但它是为我标记。@Pragnani我使用了
templist.add(adapter.list.get(overList.get(I));
并在
overList.get(I)
下面加下划线,因为get()接受int,但overList.get(I)是ArrayList。这是做什么的around@SweetieAnang
get()
方法将获取索引postList.remove(位置);mAdapter.notifyItemRemoved(位置);mAdapter.notifyItemRangeChanged(position,postList.size());
if (posList.size() >0)
i=appAdp.list.size();
while(i<=posList.size() && i<=appAdp.list.size())
{
 appAdp.list.remove(appAdp.list.get(posList.get(i)));
 i--;
}
ArrayList<yourappListtype> templist=new ArrayList<yourappListtype>();
for (int i = 0; i < posList.size(); i++)
        templist.add(appAdp.list.get(posList.get(i)));
appAdp.list.removeAll(templist);