简体   繁体   English

迭代时从列表中删除对象不起作用(使用迭代器)

[英]Removing object from a list while iterating it doesn't work (with iterator)

This is the method: 这是方法:

public void deleteEvent(View view){
        Intent intent = new Intent(this, MainActivity.class);

        for (User myUser:LocalDataBase.getEventByID(eventID).getInvitedUsersTotal()){
            for (Event myEvent:myUser.getAttendingList())
            {
                ListIterator<Event> itr_attending =myUser.getAttendingList().listIterator();
                if (myEvent.getId()==eventID){
                    itr_attending.remove();
                }

            }
        }

        Toast.makeText(getApplicationContext(), "The event was deleted",Toast.LENGTH_LONG).show();
        startActivity(intent);
}

This method is in order to delete an event from the database. 此方法是为了从数据库中删除事件。 In order to do it I need to remove it from the "attending list" of each invited user. 为此,我需要将其从每个受邀用户的“出席列表”中删除。 (If the user says he is going to the event, the event moving to this list of him). (如果用户说他要去参加活动,那么活动将移到他的这个列表中)。

Each event has a list of invited users (invitedUsersTotal) , and for each user, the method should go over the user's attending list and serach for the wanted event (myEvent.getId()==eventID) , and than delete it. 每个事件都有一个受邀用户列表(invitedUsersTotal) ,对于每个用户,该方法应遍历用户的出席列表并为所需事件(myEvent.getId()==eventID)搜索,然后删除该事件。

In the end, the event is not deleted from the attending list. 最后,该事件不会从出席者列表中删除。

What am I doing wrong here? 我在这里做错了什么?

Read the Javadoc of ListIterator.remove() : 阅读ListIterator.remove()Javadoc

Removes from the list the last element that was returned by next() or previous() 从列表中删除由next()previous()返回的最后一个元素

You're never calling next() or previous() on the list iterator, so there's nothing to remove. 您永远不会在列表迭代器上调用next()previous() ,因此无需删除任何内容。


It's not very clear what you are intending to remove from the list - but the fact that you have two iterators concurrently on the same list suggests that - if you really did remove anything - you'd get a ConcurrentModificationException anyway. 尚不清楚您打算从列表中删除什么-但是,在同一列表中同时有两个迭代器的事实表明-如果确实删除了任何内容,则无论如何都会得到ConcurrentModificationException

I think that you need to replace the inner enhanced for loop with an explicit iteration: you can't remove from a list that you're iterating with an enhanced for loop: 我认为您需要用显式迭代替换内部的增强型for循环:您不能从要增强型for循环迭代的列表中删除:

for (User myUser: ...){
  Iterator<Event> it = myUser.getAttendingList().iterator();
  while (it.hasNext()) {
    Event myEvent = it.next();
    if (myEvent.getId()==eventID){
      it.remove();
    }
  }
}

do like this 像这样

ListIterator<Event> itr_attending =myUser.getAttendingList().listIterator();
      int id = myEvent.getId()
      while(itr_attending.hasNext()){               
           if (id==eventID){
               itr_attending.remove();
           }
      }

I think you should iterate like this: 我认为您应该这样迭代:

 ListIterator<Event> itr_attending =myUser.getAttendingList().listIterator();
  // Below you are iterating
  while(itr_attending.hasNext()){
       Event event = itr_attending.next();
       int eventId = event.getEventId() // Or something like that based on event object
       if (myEvent.getId()==eventID){
           itr_attending.remove();
       }
  }

After getting iterator, you need loop with itr_attending.hasNext() . 获得迭代器后,需要使用itr_attending.hasNext()循环。 It will check if you have any next element in loop. 它将检查循环中是否有下一个元素。 And if you have, condition succeed and you can get the Event object by itr_attending.next() . 如果有,则条件成功,并且可以通过itr_attending.next()获得Event对象。 Then, do what you want to do. 然后,执行您想做的事情。

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

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