A:案例演示java
import net.allidea.bean.Student; public class Colletion_1_Array { public static void main(String[] args) { // int[] arr = new int[5]; //建立基本數據類型數組 Student[] arr = new Student[5]; //建立引用數據類型數組 arr[0] = new Student("張三", 23); //建立一個學生對象,存儲在數組第一個位置 arr[1] = new Student("李四", 24); arr[2] = new Student("王五", 25); arr[3] = new Student("趙六", 26); arr[4] = new Student("馬哥", 20); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); //toString } } }
B:畫圖演示算法
A:集合的由來數組
B:數組和集合的區別安全
區別1 :數據結構
區別2:併發
C:數組和集合何時用eclipse
D:集合繼承體系圖ide
Collection(單列集合的根接口)測試
List(有序,存取順序一致,有索引,能夠重複存儲)idea
Set(無序,不一致,無索引,不可重複存儲)
A:案例演示
B:注意:
import java.util.ArrayList; import java.util.Collection; import net.allidea.bean.Student; @SuppressWarnings({ "rawtypes", "unchecked" }) public class Colletion_2_demo { public static void main(String[] args) { // addDemo(); Collection c = new ArrayList(); //父類引用指向子類對象 c.add("a"); c.add("b"); c.add("c"); c.add("d"); c.add("e"); // c.remove("b"); //刪除指定元素 c.clear(); //清空集合 // System.out.println(c); // System.out.println(c.contains("b"));//判斷是否包含 System.out.println(c.isEmpty()); //判斷是否爲空 System.out.println(c.size()); //獲取元素的個數 } private static void addDemo() { Collection c = new ArrayList(); //父類引用指向子類對象 boolean b1 = c.add("abc"); boolean b2 = c.add(true); //自動裝箱new Boolean(true); boolean b3 = c.add(100); boolean b4 = c.add(new Student("張三",23)); // boolean b5 = c.add("abc"); System.out.println(b1); System.out.println(b2); System.out.println(b3); System.out.println(b4); System.out.println(b5); System.out.println(c.toString()); } }
A:集合的遍歷
B:案例演示
import java.util.ArrayList; import java.util.Collection; import net.allidea.bean.Student; @SuppressWarnings({ "rawtypes", "unchecked" }) public class Colletion_3_demo { public static void main(String[] args) { // demo1(); Collection c = new ArrayList(); //Object obj = new Student("張三",23); c.add(new Student("張三", 23)); c.add(new Student("李四", 24)); c.add(new Student("王五", 25)); c.add(new Student("趙六", 26)); Object[] arr = c.toArray(); //將集合轉換成數組 for (int i = 0; i < arr.length; i++) { // System.out.println(arr[i]); Student s = (Student)arr[i]; //向下轉型 System.out.println(s.getName() + "..." + s.getAge()); } } private static void demo1() { Collection c = new ArrayList(); c.add("a"); c.add("b"); c.add("c"); c.add("d"); Object[] arr = c.toArray(); //將集合轉換成數組 for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]); } } }
import java.util.ArrayList; import java.util.Collection; @SuppressWarnings({ "unchecked", "rawtypes" }) public class Collection_4_demo { public static void main(String[] args) { // addAllDemo(); // removeAllDemo(); // containsAllDemo(); Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); // c2.add("z"); boolean b = c1.removeAll(c2);//取交集,若是調用的集合改變就返回true,若是調用的集合不變就返回false System.out.println(b); System.out.println(c1); } private static void containsAllDemo() { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); // c2.add("a"); // c2.add("b"); c2.add("z"); boolean b = c1.containsAll(c2); //判斷調用的集合是否包含傳入的集合 System.out.println(b); } private static void removeAllDemo() { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("a"); c2.add("b"); c2.add("z"); boolean b = c1.removeAll(c2); //刪除的是交集內容 System.out.println(b); System.out.println(c1); } private static void addAllDemo() { Collection c1 = new ArrayList(); c1.add("a"); c1.add("b"); c1.add("c"); c1.add("d"); Collection c2 = new ArrayList(); c2.add("aa"); c2.add("bb"); c2.add("cc"); c2.add("dd"); c1.addAll(c2); //將c2總體元素添加到c1中 c1.add(c2); //將c2當作一個對象添加到c1中 System.out.println(c1); } }
A:迭代器概述
B:案例演示
import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import net.allidea.bean.Student; @SuppressWarnings({ "unchecked", "rawtypes" }) public class Collection_5_demo { public static void main(String[] args) { // demo1(); Collection c = new ArrayList(); c.add(new Student("張三",23)); //Object obj = new Student("張三",23); c.add(new Student("李四",24)); c.add(new Student("王五",25)); c.add(new Student("趙六",26)); c.add(new Student("田七",27)); /* for(Iterator it = c.iterator();it.hasNext();) { Student s = (Student)it.next(); //向下轉型 System.out.println(s.getName() + "," + s.getAge()); //獲取對象中的姓名和年齡 }*/ // 獲取迭代器 Iterator it = c.iterator(); while(it.hasNext()) { // System.out.println(it.next()); //System.out.println(((Student)(it.next())).getName() + "," + ((Student)(it.next())).getAge()); Student s = (Student)it.next(); //向下轉型 System.out.println(s.getName() + "..." + s.getAge()); } } private static void demo1() { Collection c = new ArrayList(); c.add("a"); c.add("b"); c.add("c"); c.add("d"); //對集合中的元素迭代(遍歷) Iterator it = c.iterator(); //獲取迭代器 /*boolean b1 = it.hasNext(); //判斷集合中是否有元素,有就返回true Object obj1 = it.next(); System.out.println(b1); System.out.println(obj1); boolean b2 = it.hasNext(); //判斷集合中是否有元素,有就返回true Object obj2 = it.next(); System.out.println(b2); System.out.println(obj2);*/ while(it.hasNext()) { System.out.println(it.next()); } } }
A:迭代器原理
B:迭代器源碼解析
A:List集合的特有功能概述
import java.util.ArrayList; import java.util.List; public class List_1_demo { /* * A:List集合的特有功能概述 * void add(int index,E element) * E remove(int index) * E get(int index) * E set(int index,E element)*/ public static void main(String[] args) { // demo1(); // demo2(); // demo3(); // demo4(); demo5(); } private static void demo5() { List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.set(1, "z"); System.out.println(list); //將制定位置的元素修改 } private static void demo4() { List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); // Object obj1 = list.get(1); // System.out.println(obj1); //經過索引遍歷list集合 for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } } private static void demo3() { List list = new ArrayList(); list.add(111); list.add(222); list.add(333); list.remove(0); //刪除的時候不會自動裝箱,把111當索引 System.out.println(list); } private static void demo2() { List list = new ArrayList(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); Object obj = list.remove(1); //經過索引刪除元素,將被刪除的元素返回 System.out.println(obj); System.out.println(list); } private static void demo1() { List list = new ArrayList(); //開發時直接建立子類對象 list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add(1, "f"); //index <= size且>=零均可以 // list.add(1, "e"); // list.add(10,"z"); //使用不存在的索引時,索引越界異常java.lang.IndexOutOfBoundsException System.out.println(list); } }
A:案例演示
import java.util.List; import java.util.ArrayList; import net.allidea.bean.Student; @SuppressWarnings({ "unchecked", "rawtypes" }) public class List_2_demo { public static void main(String[] args) { List list = new ArrayList(); list.add(new Student("張三",23)); //Object obj = new Student("張三",23); list.add(new Student("李四",24)); list.add(new Student("王五",25)); list.add(new Student("趙六",26)); list.add(new Student("田七",27)); for (int i = 0; i < list.size(); i++) { //經過索引獲取每個元素 // System.out.println(list.get(i)); Student s = (Student)list.get(i); System.out.println(s.getName() + "..." + s.getAge()); } } }
A:案例演示
List list = new ArrayList<>(); list.add("a"); //Object obj = new String(); list.add("b"); list.add("c"); list.add("world"); list.add("d"); list.add("e"); Iterator it = list.iterator(); //獲取迭代器 while(it.hasNext()) { String str = (String)it.next();//向下轉型 if("world".equals(str)) { list.add("javaee"); //遍歷的同時在增長元素,併發修改ConcurrentModificationException } System.out.println(list); }
B:ConcurrentModificationException出現
C:解決方案
ListIterator lit = list.listIterator(); //獲取迭代器,list集合特有的 while(lit.hasNext()) { String str = (String)lit.next();//向下轉型 if("world".equals(str)) { // list.add("javaee"); //遍歷的同時在增長元素,併發修改ConcurrentModificationException lit.add("javaee"); } System.out.println(list); }
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class List_4_ListIterator { public static void main(String[] args) { List list = new ArrayList(); list.add("a"); //Object obj = new String(); list.add("b"); list.add("c"); list.add("world"); list.add("d"); list.add("e"); ListIterator lit = list.listIterator(); /*while(lit.hasNext()) { System.out.println(lit.next()); }*/ System.out.println("------------"); while(lit.hasPrevious()) { System.out.println(lit.previous()); } } }
B:Vector類特有功能
C:案例演示
import java.util.Enumeration; import java.util.Vector; public class List_5_Vector { public static void main(String[] args) { Vector v = new Vector(); //建立集合對象,List的子類 v.addElement("a"); v.addElement("b"); v.addElement("c"); v.addElement("d"); //Vector迭代 Enumeration en = v.elements(); //獲取枚舉 while(en.hasMoreElements()) { //判斷集合中是否有元素 System.out.println(en.nextElement());//獲取集合中的元素 } } }
A:數組
B:鏈表
A:List的三個子類的特色
ArrayList:
Vector:
LinkedList:
Vector和ArrayList的區別
ArrayList和LinkedList的區別
B:List有三個兒子,咱們到底使用誰呢?