取下ArrayList中元素的所有实例 [英] Remove all occurrences of an element from ArrayList

查看:226
本文介绍了取下ArrayList中元素的所有实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的java.util.ArrayList ,我想删除某个特定元素的所有事件。

 列表<串GT; L =新的ArrayList<串GT;();
    l.add(第一);
    l.add(第一);
    l.add(第二);    l.remove(第一);

这是仅去除第一次出现。但我想以后要删除所有出现l.remove(第一); 我希望被排除只与价值的第二之列。我发现谷歌上搜索通过它可以通过创建新的列表并调用 list.removeAll(newList)来实现。但是,它可以删除所有没有出现新创建列表或有任何可用的API来实现呢?


解决方案

  l.removeAll(Collections.singleton(第一));

I am using java.util.ArrayList, I want to remove all the occurrences of a particular element.

    List<String> l = new ArrayList<String>();
    l.add("first");
    l.add("first");
    l.add("second");

    l.remove("first");

It's removing only the first occurrence. But I want all the occurrences to be removed after l.remove("first"); I expect list to be left out only with the value "second". I found by googling that it can be achieved by creating new list and calling list.removeAll(newList). But is it possible to remove all occurrences without creating new list or is there any API available to achieve it ?

解决方案

l.removeAll(Collections.singleton("first"));

这篇关于取下ArrayList中元素的所有实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆