package package1; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Demo3 { public static void main(String[] args) { List<String> all=new ArrayList<String>(); //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<String> 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<Student> 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<String> list = new ArrayList<String>(); 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<String> list = new ArrayList<String>(); System.out.println("原集合"); for(int i=0;i<array.length;i++) { System.out.println(array[i]+","); } //循環把數組中的值添加到集合中 for(int i=0;i<3;i++) { list.add(array[i]); } System.out.println(); System.out.println("添加到集合後"); System.out.println(list); //輸出集合 for(String string:list) { System.out.println(string); } System.out.println("-------------------"); //返回列表中指定位置的元素 System.out.println(list.get(2)); //返回指定元素第一次出現的索引,若是存在則返回索引下標,不然返回-1 System.out.println(list.indexOf("小王,男,1980-03-08")); //返回列表中最後一次出現的索引 System.out.println(list.lastIndexOf("小ma,男,1980-03-08")); //刪除 //System.out.println(list.remove("小ma,男,1980-03-08"); //返回列表中的元素數目 System.out.println(list.size()); System.out.println("-------------------"); System.out.println("刪除下標爲2的值"); list.remove(1); //刪除全部 //list.removeAll(list); System.out.println(list); System.out.println("刪除後"); System.out.println(list); //判斷list中是否包含該元素,返回true或false System.out.println(list.contains("小ss,男,1980-03-08")); } }