开发者社区 问答 正文

怎么样边遍历边移除 Collection 中的元素?

怎么样边遍历边移除 Collection 中的元素?

展开
收起
叫我饭啊啊 2021-10-25 14:25:33 586 分享 版权
1 条回答
写回答
取消 提交回答
  • 边遍历边修改 Collection 的唯一正确方式是使用 Iterator.remove() 方法,如下:

    Iterator it = list.iterator();

    while(it.hasNext()){

    // do something

    it.remove();

    }

    2021-10-25 14:26:03
    赞同 展开评论
问答地址: