简体   繁体   English

从Java ArrayList删除项目时接收IndexOutOfBoundsException

[英]Receiving IndexOutOfBoundsException when removing item from Java ArrayList

I have a list of values. 我有一个值列表。 I want to remove the list item when check box is unclicked: 取消选中复选框后,我想删除列表项:

ArrayList<SalesRoutes> routeList = new ArrayList<SalesRoutes>();
ArrayList<String> selectedRoutes = new ArrayList<String>();
 routeList =getSalesRoute();
for (int i = 0; i < routeList.size(); i++) {
         CheckBox ch = new CheckBox(this);
   ch.setId(i);
   ch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                    if(arg0.isChecked()){   
                        Log.i("add", routeList.get(arg0.getId()).getRouteCode());
                        selectedRoutes.add(routeList.get(arg0.getId()).getRouteCode());
                        System.out.println("----add ----" + selectedRoutes);
                    }else {
                        selectedRoutes.remove(arg0.getId());
                        Log.i("remove", routeList.get(arg0.getId()).getRouteCode());
                        System.out.println("----remove ----" + selectedRoutes);
                    }
                }
            });
   }

Here I got IndexOutOfBoundsException because selectedRoutes is selected CheckBox values 在这里我得到了IndexOutOfBoundsException因为selectedRoutes被选中了CheckBox值

   selectedRoutes   
   [R0002]

routeList displays a list of routes on the screen. routeList在屏幕上显示路由列表。 It fetches the route from a db. 它从数据库获取路由。

Example routeList: routeList示例:

  Route List
  R0001
  R0002   // selected this one ID is 1
  R0003

calling remove from selectedRoutes(1). 从selectedRoutes(1)调用remove。

   selectedRoutes.remove(arg0.getId());

Here selectedRoutes only contains one record, which means there is no index 1. 这里的selectedRoutes只包含一个记录,这意味着没有索引1。

How can I remove this? 我该如何删除?

The problem is with this section of code: 问题在于这段代码:

selectedRoutes.remove(arg0.getId());
Log.i("remove", routeList.get(arg0.getId()).getRouteCode());
System.out.println("----remove ----" + selectedRoutes);

You're hinging that on the (usually incorrect) assumption that the index of your route in your list of potential routes is the same as the index of your route in the list of selected routes. 您(通常是错误的)假设是,潜在路线列表中的路线索引与选定路线列表中的路线索引相同。 Instead, you'll want to get the route code of the route in your potential list of routes at that index, then iterate through your list of selected routes (which are actually just Strings containing route codes), and remove the index where the two match up. 相反,您将需要在该索引的潜在路由列表中获取该路由的路由代码,然后遍历所选路由列表(实际上只是包含路由代码的字符串),并删除两个索引配对。 Code would look something like this: 代码如下所示:

String routeCode = routeList.get(arg0.getId()).getRouteCode();
index = -1;
for(int i = 0; i < selectedRoutes.size(); i++) {
    if(routeCode.equals(selectedRoutes.get(i)) {
        index = i;
            break;
    }
}
if(index > -1)
    selectedRoutes.remove(index);
Log.i("remove", routeCode);
System.out.println("----remove ----" + selectedRoutes);

It would be much easier if you created selectedRoutes to be a collection of the same objects ArrayList<SalesRoutes> selectedRoutes . 如果将selectedRoutes创建为相同对象ArrayList<SalesRoutes> selectedRoutes的集合,那会容易得多。 Since both collections would contain the references to the same object you could remove the object by its reference: 由于两个集合都包含对同一对象的引用,因此您可以通过引用删除该对象:

salectedRoutes.remove(routeList.get(arg0.getId()));

Calling ArrayList.remove(int location) will remove the object at that location in the array. 调用ArrayList.remove(int location)将删除数组中该位置的对象。 It does not remove a object by it's Id value. 不会通过其ID值移除对象。

It appears you are adding an object to the arraylist at an index position returned by .getRouteCode() 看来您是在.getRouteCode()返回的索引位置处将对象添加到arraylist中

But then you are trying to remove an object at an index position returned by .getId(). 但是,然后您尝试在.getId()返回的索引位置处删除对象。

The two don't sync up. 两者不同步。

您已经混合使用ArrayList.remove(int index)ArrayList.remove(Object o)

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

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