Remove entries from the list using iterator

前端 未结 4 878
无人及你
无人及你 2020-12-21 09:30

I need to write a simple function that will delete all entries in the List that contains objects of the class Elem. I wrote the function remo

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 09:55

    The code doesn't compile. What is this.tokens?

    Anyway, if you want to remove an element while iterating, you must do it using the iterator's remove method:

    itr.next();
    itr.remove();
    

    Your removeAllElements method could just do this.elements.clear(), though. Much more straightforward and efficient.

提交回复
热议问题