java:ArrayList能一次删除多个元素吗?

dingcj_scau:
根据索引值删除,也行吗?

是可以的。ArrayList是实现了List接口,而List的底层实现是数组,提供了以下两个方法:
list.remove(index);移除单个元素;
list.removeAll(c);移除多个元素,而参数c也要求是一个集合。
在第二个方法中,如果c中的元素和list中的元素相等,那么list就将其移除。那么怎么判断c中的元素和list中的元素相等呢?当然如果我创造一个元素,分别调用add(e)的方法将其添加到list和c中,那么它们是肯定相等的。但是如果不是这样,那么集合会根据元素的
public boolean equals(Object obj);这个方法来判断两个元素是否相等。例如我们创建一个类City:
public class City {
private int id;
private String cityname;
public City(int id,String cityname) {
this.id = id;
this.cityname = cityname;
}
}
下面进行测试:
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
List<City> list = new ArrayList<City>();
list.add(new City(1,"guangzhou"));
list.add(new City(2,"shanghai"));
list.add(new City(3,"beijing"));
List<City> subList = new ArrayList<City>();
subList.add(new City(1,"guangzhou"));
subList.add(new City(2,"beijing"));
System.out.println(list.removeAll(subList));//输出结果是false;
System.out.println(list.size());//输出结果是3;
}
}
接下来我们改写City:
public class City {
private int id;
private String cityname;
public City(int id,String cityname) {
this.id = id;
this.cityname = cityname;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof City){
City city = (City) obj;
return cityname.equals(city.cityname);
}
return false;
}
}
再进行测试:
public class Test {

/**
* @param args
*/
public static void main(String[] args) {
List<City> list = new ArrayList<City>();
list.add(new City(1,"guangzhou"));
list.add(new City(2,"shanghai"));
list.add(new City(3,"beijing"));
List<City> subList = new ArrayList<City>();
subList.add(new City(1,"guangzhou"));
subList.add(new City(2,"beijing"));
System.out.println(list.removeAll(subList));//输出结果是true;
System.out.println(list.size());//输出结果是1;
}
}
其中的原理和判断set集合的元素是否重复是一样的。你应该明白了吧?
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-06-21

  ArrayList有两个函数可以一次删除多个元素,一个是remove,例如:list1.removeAll(list2);表示删除包含在list1中所有和list2中一样的元素。第二个是:retainAll,例如: list1.retainAll(list3);表示吧list1中不包含在list3中的元素全部删除。下面是一个具体的演示实例:

public class ArrayRemoveTest
{
    public static void print(ArrayList<Integer> list)
    {
        for(int a:list)
            System.out.print(a+" ");
        System.out.println("\n");
    }
    public static void main(String[] args)
    {
        ArrayList<Integer> list1 = new ArrayList<Integer>();
        ArrayList<Integer> list2 = new ArrayList<Integer>();
        ArrayList<Integer> list3 = new ArrayList<Integer>();
        for(int i = 0;i < 30;i++)
            list1.add(i);
        for(int i = 11;i < 30;i++)
            list2.add(i);
        for(int i = 0;i < 10;i++)
            list3.add(i);
        
        print(list1);
        list1.removeAll(list2);
        print(list1);
        list1.retainAll(list3);
        print(list1);
    }
}

第2个回答  2011-03-12
Add、AddRange、Remove、RemoveAt、RemoveRange、Insert、InsertRange

Add方法用于添加一个元素到当前列表的末尾
AddRange方法用于添加一批元素到当前列表的末尾
Remove方法用于删除一个元素,通过元素本身的引用来删除
RemoveAt方法用于删除一个元素,通过索引值来删除
RemoveRange用于删除一批元素,通过指定开始的索引和删除的数量来删除
Insert用于添加一个元素到指定位置,列表后面的元素依次往后移动
InsertRange用于从指定位置开始添加一批元素,列表后面的元素依次往后移动

Clear方法用于清除现有所有的元素
Contains方法用来查找某个对象在不在列表之中
第3个回答  2011-03-12
用clear()可以删除全部,removeAll(Collection<?> c) 也可以删除多个

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网