package tenth_day_作业2;
import java.util.ArrayList;
import java.util.List;
//1、remove(int index);//删除指定位置的元素
//2、remove(Object o);//删除指定对象,考查删除对象的规则是什么?
//3、removeAll(Collection col);//删除指定集合中的所有元素。
//4、contains(Object o);//是否包含
//5、contains(Collection col);//是否包含集合。
public class Demo {
public static void main(String[] args) {
List<Student> list = new ArrayList<Student>();
Student s1 = new Student("s-1");
Student s2 = new Student("s-2");
Student s3 = new Student("s-3");
Student s4 = new Student("s-4");
Student s5 = new Student("s-5");
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
System.out.println("删除第一个学生");
list.remove(0);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
System.out.println("通过remove(object o)删除对象");
list.remove(s3);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
//boolean contains(Object o
//如果列表包含指定的元素,返回true
//boolean containAll(Collection<?> c)
//如果列表包含指定collection的所有元素,则返回true
System.out.println("list是否包含s1呢——"+list.contains(s1));
System.out.println("list是否包含s4呢——"+list.contains(s4));
List<Student> list2=new ArrayList<Student>();
list2.add(s2);
list2.add(s5);
System.out.println("list是否包含containsAll(list2):"+list.containsAll(list2));
System.out.println("list是否包含contains(list2):"+list.contains(list2));
List<Student> list3=new ArrayList<Student>();
list3.add(s3);
list3.add(s5);
System.out.println("list是否包含list3:"+list.containsAll(list3));
// boolean removeAll(Collection<?> c)从列表中移除指定collection中包含的所有元素(可选操作)
list.removeAll(list3);
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).getName());
}
}
}
转载于:https://blog.51cto.com/11227677/1786587