ArrayList中有remove 方法和 removeAll方法,
ArrayList中不仅继承了接口Collection中的remove方法,而且还扩展了remove方法。
Collection中声明的接口为 public boolean remove(Object o)
public boolean removeAll(Collection<?> c)
ArrayList中含有的方法为public E remove(int index)
实际编程中可能会通过一个Collection来调用remove接口,这种情况下不会报错,但是程序什么也不做。
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
public class ArrayList01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> allList = null;
Collection<String> allCollection = null;
allList = new ArrayList<String>();
allCollection = new ArrayList<String>();
allList.add("hello");
allList.add(0, "world");
System.out.println(allList);
allCollection.add("Yes");
allCollection.add("Good");
allList.addAll(allCollection);
allList.addAll(0, allCollection);
System.out.println(allList);
allList.remove(allCollection);
//allList.removeAll(allCollection);
System.out.println(allList);
}
}
实际编程可以插入任意对象,但是如果想要通过remove(object o)来删除某个对象,这个对象必须是系统自定义的对象,如果不是的话,需要在类中覆写Object类的equals()及hashCode()方法。
原文:http://www.cnblogs.com/aituming/p/4792354.html