package tenth_day_做業2; import java.util.ArrayList; import java.util.List; //一、remove(int index);//刪除指定位置的元素 //二、remove(Object o);//刪除指定對象,考查刪除對象的規則是什麼? //三、removeAll(Collection col);//刪除指定集合中的全部元素。 //四、contains(Object o);//是否包含 //五、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()); } } }