package package1;import java.util.ArrayList;import java.util.Iterator;import java.util.List;public class Demo3 { public static void main(String[] args) { Listall=new ArrayList (); //hashset无序但不允许重复,treeset不允许重复 all.add("a"); all.add("b"); all.add("c"); all.add(2,"d");//把d插入下标为2的位置 all.set(0, "hello");//把下标元素为0的值替换为hello //System.out.println(all); System.out.println("使用迭代法"); Iterator it=all.iterator(); while(it.hasNext()) { String name=it.next(); System.out.println(name); } System.out.println("使用增强for循环"); for(String name:all) { System.out.println(name); } System.out.println("**************"); //判断是否有lisi这个值,有则返回所对应的索引,没有则返回-1; System.out.println(all.lastIndexOf("lisi")); System.out.println(all.remove("hello"));//移除hello System.out.println(all.remove(0));//移除下标为0的值 System.out.println("======="); System.out.println(all.size());//集合返回个数 System.out.println(all.contains("hello"));//是否包含hello System.out.println(all.get(0));//获取下标为0的值 System.out.println(all.isEmpty());//清除前 all.clear();//清除 System.out.println(all.isEmpty());//清除后 }}实例:package package1;import java.util.ArrayList;import java.util.List;public class Demo4 {public static void main(String[] args) { List list=new ArrayList<>(); Student student1=new Student("zhangsan", 20); Student student2=new Student("zl", 20); Student student3=new Student("wangwu", 20); Student student4=new Student("lisi", 20); list.add(student1); list.add(student2); list.add(student3); list.add(student4); for(Student stu:list) { System.out.println("姓名:"+stu.getName()+"年龄:"+stu.getAge()); }}}class Student{ String name; int age; public Student(String name,int age) { this.name=name; this.age=age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }}遍历List集合的三种方法List list = new ArrayList ();list.add("aaa");list.add("bbb");list.add("ccc");方法一:超级for循环遍历for(String attribute : list) { System.out.println(attribute);}方法二:对于ArrayList来说速度比较快, 用for循环, 以size为条件遍历:for(int i = 0 ; i < list.size() ; i++) { system.out.println(list.get(i));}方法三:集合类的通用遍历方式, 从很早的版本就有, 用迭代器迭代Iterator it = list.iterator();while(it.hasNext()) { System.ou.println(it.next);}练习:package package1;import java.util.ArrayList;public class Demo9 { public static void main(String [] args) { String[] array ={"小王,男,1980-03-08","小zhang,男,1980-03-08","小ma,男,1980-03-08"}; ArrayList list = new ArrayList (); System.out.println("原集合"); for(int i=0;i