今日內容介紹
一、集合
二、Iterator迭代器
三、加強for循環
四、泛型java
*A:集合使用的回顧 *a.ArrayList集合存儲5個int類型元素 public static void main(String[] args) { ArrayList<Integer> list = new ArrayList<Integer>(); list.add(111); list.add(222); list.add(333); list.add(444); list.add(555); for(int i=0; i<list.size(); i++){ System.out.println(list.get(i)); } } *b.ArrayList集合存儲5個Person類型元素 public static void main(String[] args) { ArrayList<Person> list = new ArrayList<Person>(); list.add(new Person(「小強」)); list.add(new Person(「老王」)); list.add(new Person(「小虎」)); list.add(new Person(「小澤」)); list.add(new Person(「小紅」)); for(int i=0; i<list.size(); i++){ Person p = list.get(i); System.out.println(p); } }
集合,集合是java中提供的一種容器,能夠用來存儲多個數據。 在前面的學習中,咱們知道數據多了,可使用數組存放或者使用ArrayList集合進行存放數據。 那麼,集合和數組既然都是容器,它們有啥區別呢? 數組的長度是固定的。集合的長度是可變的。 集合中存儲的元素必須是引用類型數據
A:集合繼承關係圖 a:ArrayList的繼承關係: 查看ArrayList類發現它繼承了抽象類AbstractList同時實現接口List,而List接口又繼承了Collection接口。Collection接口爲最頂層集合接口了。 源代碼: interface List extends Collection { } public class ArrayList extends AbstractList implements List{ } b:集合繼承體系 這說明咱們在使用ArrayList類時,該類已經把全部抽象方法進行了重寫。那麼,實現Collection接口的全部子類都會進行方法重寫。 Collecton接口經常使用的子接口有:List接口、Set接口 List接口經常使用的子類有:ArrayList類、LinkedList類 Set接口經常使用的子類有:HashSet類、LinkedHashSet類 Collection 接口 | ---------------------------------------------------------------- | | List接口 Set接口 | | ---------------- ------------- | | | | ArrayList類 LinkedList類 HashSet類 LinkedHashSet類
A:集合Collection的方法 /* * Collection接口中的方法 * 是集合中全部實現類必須擁有的方法 * 使用Collection接口的實現類,程序的演示 * ArrayList implements List * List extends Collection * 方法的執行,都是實現的重寫 */ public class CollectionDemo { public static void main(String[] args) { function_2(); } /* Collection接口方法 * Object[] toArray() 集合中的元素,轉成一個數組中的元素, 集合轉成數組 * 返回是一個存儲對象的數組, 數組存儲的數據類型是Object */ private static void function_2() { Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("itcast"); coll.add("itheima"); coll.add("money"); coll.add("123"); Object[] objs = coll.toArray(); for(int i = 0 ; i < objs.length ; i++){ System.out.println(objs[i]); } } /* * 學習Java中三種長度表現形式 * 數組.length 屬性 返回值 int * 字符串.length() 方法,返回值int * 集合.size()方法, 返回值int */ /* * Collection接口方法 * boolean contains(Object o) 判斷對象是否存在於集合中,對象存在返回true * 方法參數是Object類型 */ private static void function_1() { Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("itcast"); coll.add("itheima"); coll.add("money"); coll.add("123"); boolean b = coll.contains("itcast"); System.out.println(b); } /* * Collection接口的方法 * void clear() 清空集合中的全部元素 * 集合容器自己依然存在 */ public static void function(){ //接口多態的方式調用 Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("bcd"); System.out.println(coll); coll.clear(); System.out.println(coll); } }
A:05集合Collection的remove方法 /* * Collection接口方法 * boolean remove(Object o)移除集合中指定的元素 */ private static void function_3(){ Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("money"); coll.add("itcast"); coll.add("itheima"); coll.add("money"); coll.add("123"); System.out.println(coll); boolean b = coll.remove("money"); System.out.println(b); System.out.println(coll); }
A:迭代器概述:數組
a:java中提供了不少個集合,它們在存儲元素時,採用的存儲方式不一樣。 咱們要取出這些集合中的元素,可經過一種通用的獲取方式來完成。 b:Collection集合元素的通用獲取方式:在取元素以前先要判斷集合中有沒有元素, 若是有,就把這個元素取出來,繼續在判斷,若是還有就再取出出來。一直把集合中的 全部元素所有取出。這種取出方式專業術語稱爲迭代。 c:每種集合的底層的數據結構不一樣,例如ArrayList是數組,LinkedList底層 是鏈表,可是不管使用那種集合,咱們都會有判斷是否有元素 以及取出裏面的元素的動做,那麼Java爲咱們提供一個迭代器定義了統一的判斷元素和取元素的方法
*A:迭代器的實現原理安全
/* * 集合中的迭代器: * 獲取集合中元素方式 * 接口 Iterator : 兩個抽象方法 * boolean hasNext() 判斷集合中還有沒有能夠被取出的元素,若是有返回true * next() 取出集合中的下一個元素 * * Iterator接口,找實現類. * Collection接口定義方法 * Iterator iterator() * ArrayList 重寫方法 iterator(),返回了Iterator接口的實現類的對象 * 使用ArrayList集合的對象 * Iterator it =array.iterator(),運行結果就是Iterator接口的實現類的對象 * it是接口的實現類對象,調用方法 hasNext 和 next 集合元素迭代 */
*A:迭代器的代碼實現數據結構
public class IteratorDemo { public static void main(String[] args) { Collection<String> coll = new ArrayList<String>(); coll.add("abc1"); coll.add("abc2"); coll.add("abc3"); coll.add("abc4"); //迭代器,對集合ArrayList中的元素進行取出 //調用集合的方法iterator()獲取出,Iterator接口的實現類的對象 Iterator<String> it = coll.iterator(); //接口實現類對象,調用方法hasNext()判斷集合中是否有元素 //boolean b = it.hasNext(); //System.out.println(b); //接口的實現類對象,調用方法next()取出集合中的元素 //String s = it.next(); //System.out.println(s); //迭代是反覆內容,使用循環實現,循環的條件,集合中沒元素, hasNext()返回了false while(it.hasNext()){ String s = it.next(); System.out.println(s); } } }
A:迭代器的執行過程學習
a:迭代器的原理: while(it.hasNext()) { System.out.println(it.next()); } //cursor記錄的索引值不等於集合的長度返回true,不然返回false public boolean hasNext() { return cursor != size; //cursor初值爲0 } //next()方法做用: //①返回cursor指向的當前元素 //②cursor++ public Object next() { int i = cursor; cursor = i + 1; return elementData[lastRet = i]; } b:for循環迭代寫法: for (Iterator<String> it2 = coll.iterator(); it2.hasNext(); ) { System.out.println(it2.next()); }
A:集合迭代中的轉型測試
a:在使用集合時,咱們須要注意如下幾點: 集合中存儲其實都是對象的地址。 集合中能夠存儲基本數值嗎?jdk1.5版本之後能夠存儲了。 由於出現了基本類型包裝類,它提供了自動裝箱操做(基本類型對象),這樣,集合中的元素就是基本數值的包裝類對象。 b:存儲時提高了Object。取出時要使用元素的特有內容,必須向下轉型。 Collection coll = new ArrayList(); coll.add("abc"); coll.add("aabbcc"); coll.add("shitcast"); Iterator it = coll.iterator(); while (it.hasNext()) { //因爲元素被存放進集合後所有被提高爲Object類型 //當須要使用子類對象特有方法時,須要向下轉型 String str = (String) it.next(); System.out.println(str.length()); } 注意:若是集合中存放的是多個對象,這時進行向下轉型會發生類型轉換異常。 c:Iterator接口也可使用<>來控制迭代元素的類型的。代碼演示以下: Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("aabbcc"); coll.add("shitcast"); Iterator<String> it = coll.iterator(); while (it.hasNext()) { String str = it.next(); //當使用Iterator<String>控制元素類型後,就不須要強轉了。獲取到的元素直接就是String類型 System.out.println(str.length()); }
*A:加強for循環遍歷數組code
a:格式: /* * JDK1.5新特性,加強for循環 * JDK1.5版本後,出現新的接口 java.lang.Iterable * Collection開是繼承Iterable * Iterable做用,實現加強for循環 * * 格式: * for( 數據類型 變量名 : 數組或者集合 ){ * sop(變量); * } */ public static void function_1(){ //for對於對象數組遍歷的時候,可否調用對象的方法呢 String[] str = {"abc","itcast","cn"}; for(String s : str){ System.out.println(s.length()); } } /* * 實現for循環,遍歷數組 * 好處: 代碼少了,方便對容器遍歷 * 弊端: 沒有索引,不能操做容器裏面的元素 */ public static void function(){ int[] arr = {3,1,9,0}; for(int i : arr){ System.out.println(i+1); } System.out.println(arr[0]); }
A:加強for循環遍歷集合 /* * 加強for循環遍歷集合 * 存儲自定義Person類型 */ public static void function_2(){ ArrayList<Person> array = new ArrayList<Person>(); array.add(new Person("a",20)); array.add(new Person("b",10)); for(Person p : array){ System.out.println(p);// System.out.println(p.toString()); } }
A:泛型的引入對象
在前面學習集合時,咱們都知道集合中是能夠存聽任意對象的, 只要把對象存儲集合後,那麼這時他們都會被提高成Object類型。 當咱們在取出每個對象,而且進行相應的操做,這時必須採用類型轉換。好比下面程序: public class GenericDemo { public static void main(String[] args) { List list = new ArrayList(); list.add("abc"); list.add("itcast"); list.add(5);//因爲集合沒有作任何限定,任何類型均可以給其中存放 //至關於:Object obj=new Integer(5); Iterator it = list.iterator(); while(it.hasNext()){ //須要打印每一個字符串的長度,就要把迭代出來的對象轉成String類型 String str = (String) it.next();//String str=(String)obj; //編譯時期僅檢查語法錯誤,String是Object的兒子能夠向下轉型 //運行時期String str=(String)(new Integer(5)) //String與Integer沒有父子關係因此轉換失敗 //程序在運行時發生了問題java.lang.ClassCastException System.out.println(str.length()); } } }
A:泛型的定義和使用排序
/* * JDK1.5 出現新的安全機制,保證程序的安全性 * 泛型: 指明瞭集合中存儲數據的類型 <數據類型> */ public class GenericDemo { public static void main(String[] args) { function(); } public static void function(){ Collection<String> coll = new ArrayList<String>(); coll.add("abc"); coll.add("rtyg"); coll.add("43rt5yhju"); // coll.add(1); Iterator<String> it = coll.iterator(); while(it.hasNext()){ String s = it.next(); System.out.println(s.length()); } } }
A:Java中的僞泛型: 泛型只在編譯時存在,編譯後就被擦除,在編譯以前咱們就能夠限制集合的類型,起到做用 例如:ArrayList<String> al=new ArrayList<String>(); 編譯後:ArrayList al=new ArrayList();
A:泛型類:繼承
a:定義格式: 修飾符 class 類名<表明泛型的變量> { } 例如,API中的ArrayList集合: class ArrayList<E>{ public boolean add(E e){ } public E get(int index){ } } b:使用格式: 建立對象時,肯定泛型的類型 例如,ArrayList<String> list = new ArrayList<String>(); 此時,變量E的值就是String類型 class ArrayList<String>{ public boolean add(String e){ } public String get(int index){ } } 例如,ArrayList<Integer> list = new ArrayList<Integer>(); 此時,變量E的值就是Integer類型 class ArrayList<Integer>{ public boolean add(Integer e){ } public Integer get(int index){ } }
A:泛型的方法
a:定義格式:修飾符 <表明泛型的變量> 返回值類型 方法名(參數){ } b:泛型方法的使用: 1:例如,API中的ArrayList集合中的方法: public <T> T[] toArray(T[] a){ } //該方法,用來把集合元素存儲到指定數據類型的數組中,返回已存儲集合元素的數組 使用格式:調用方法時,肯定泛型的類型 例如: ArrayList<String> list = new ArrayList<String>(); String[] arr = new String[100]; String[] result = list.toArray(arr); 此時,變量T的值就是String類型。變量T,能夠與定義集合的泛型不一樣 public <String> String[] toArray(String[] a){ } 例如: ArrayList<String> list = new ArrayList<String>(); Integer[] arr = new Integer[100]; Integer [] result = list.toArray(arr); 此時,變量T的值就是Integer類型。變量T,能夠與定義集合的泛型不一樣 public <Integer> Integer[] toArray(Integer[] a){ }
A:泛型的接口:
/* * 帶有泛型的接口 * * public interface List <E>{ * abstract boolean add(E e); * } * * 實現類,先實現接口,不理會泛型 * public class ArrayList<E> implements List<E>{ * } * 調用者 : new ArrayList<String>() 後期建立集合對象的時候,指定數據類型 * * * 實現類,實現接口的同時,也指定了數據類型 * public class XXX implements List<String>{ * } * new XXX() */ public class GenericDemo2 { }
A:泛型的好處
a:將運行時期的ClassCastException,轉移到了編譯時期變成了編譯失敗。 b:避免了類型強轉的麻煩。 演示下列代碼: public class GenericDemo { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("abc"); list.add("itcast"); //list.add(5);//當集合明確類型後,存放類型不一致就會編譯報錯 //集合已經明確具體存放的元素類型,那麼在使用迭代器的時候,迭代器也一樣會知道具體遍歷元素類型 Iterator<String> it = list.iterator(); while(it.hasNext()){ String str = it.next(); System.out.println(str.length()); //當使用Iterator<String> //控制元素類型後,就不須要強轉了。獲取到的元素直接就是String類型 } } }
A:泛型的通配符
/* * 泛型的通配符 */ public class GenericDemo { public static void main(String[] args) { ArrayList<String> array = new ArrayList<String>(); HashSet<Integer> set = new HashSet<Integer>(); array.add("123"); array.add("456"); set.add(789); set.add(890); iterator(array); iterator(set); } /* * 定義方法,能夠同時迭代2個集合 * 參數: 怎麼實現 , 不能寫ArrayList,也不能寫HashSet * 參數: 或者共同實現的接口 * 泛型的通配,匹配全部的數據類型 ? */ public static void iterator(Collection<?> coll){ Iterator<?> it = coll.iterator(); while(it.hasNext()){ //it.next()獲取的對象,什麼類型 System.out.println(it.next()); } } }
A:泛型的限定
/* * 將的酒店員工,廚師,服務員,經理,分別存儲到3個集合中 * 定義方法,能夠同時遍歷3集合,遍歷三個集合的同時,能夠調用工做方法 */ import java.util.ArrayList; import java.util.Iterator; public class GenericTest { public static void main(String[] args) { //建立3個集合對象 ArrayList<ChuShi> cs = new ArrayList<ChuShi>(); ArrayList<FuWuYuan> fwy = new ArrayList<FuWuYuan>(); ArrayList<JingLi> jl = new ArrayList<JingLi>(); //每一個集合存儲本身的元素 cs.add(new ChuShi("張三", "後廚001")); cs.add(new ChuShi("李四", "後廚002")); fwy.add(new FuWuYuan("翠花", "服務部001")); fwy.add(new FuWuYuan("酸菜", "服務部002")); jl.add(new JingLi("小名", "董事會001", 123456789.32)); jl.add(new JingLi("小強", "董事會002", 123456789.33)); // ArrayList<String> arrayString = new ArrayList<String>(); iterator(jl); iterator(fwy); iterator(cs); } /* * 定義方法,能夠同時遍歷3集合,遍歷三個集合的同時,能夠調用工做方法 work * ? 通配符,迭代器it.next()方法取出來的是Object類型,怎麼調用work方法 * 強制轉換: it.next()=Object o ==> Employee * 方法參數: 控制,能夠傳遞Employee對象,也能夠傳遞Employee的子類的對象 * 泛型的限定 本案例,父類固定Employee,可是子類能夠無限? * ? extends Employee 限制的是父類, 上限限定, 能夠傳遞Employee,傳遞他的子類對象 * ? super Employee 限制的是子類, 下限限定, 能夠傳遞Employee,傳遞他的父類對象 */ public static void iterator(ArrayList<? extends Employee> array){ Iterator<? extends Employee> it = array.iterator(); while(it.hasNext()){ //獲取出的next() 數據類型,是什麼Employee Employee e = it.next(); e.work(); } } }
1.分析如下需求,並用代碼實現:
(1)有以下代碼: List<String> list = new ArrayList<>(); list.add("a"); list.add("a"); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("d"); list.add("d"); list.add("d"); list.add("d"); list.add("d"); System.out.println(frequency(list, "a")); // 3 System.out.println(frequency(list, "b")); // 2 System.out.println(frequency(list, "c")); // 1 System.out.println(frequency(list, "d")); // 5 System.out.println(frequency(list, "xxx")); // 0 (2)定義方法統計集合中指定元素出現的次數,如"a" 3,"b" 2,"c" 1
代碼:
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class HomeWork_01 { public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("a"); list.add("a"); list.add("a"); list.add("b"); list.add("b"); list.add("c"); list.add("d"); list.add("d"); list.add("d"); list.add("d"); list.add("d"); System.out.println(frequency(list, "a")); // 3 System.out.println(frequency(list, "b")); // 2 System.out.println(frequency(list, "c")); // 1 System.out.println(frequency(list, "d")); // 5 // System.out.println(frequency(list, "xxx")); // 0 //(2)定義方法統計集合中指定元素出現的次數,如"a" 3,"b" 2,"c" 1 } private static String frequency(List<String> list, String string) { Iterator<String> it = list.iterator(); int count = 0; while(it.hasNext()){ if(it.next().equals(string)){ count ++; } } System.getProperties(); return "\""+string+"\"" + " " + count; } }
2.分析如下需求,並用代碼實現:
(1)有以下代碼: public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("b"); list.add("f"); list.add("e"); list.add("c"); list.add("a"); list.add("d"); sort(list); System.out.println(list); // a, b, c, d, e, f } (2)要求對集合中添加的元素排序
代碼:
import java.util.ArrayList; import java.util.List; //排序 public class HomeWork_02 { public static void main(String[] args) { List<String> list = new ArrayList<>(); list.add("b"); list.add("f"); list.add("e"); list.add("c"); list.add("a"); list.add("d"); sort(list); System.out.println(list); // a, b, c, d, e, f } private static void sort(List<String> list) { for (int a = 0; a < list.size(); a++) { for (int b = 0; b < list.size(); b++) { if (list.get(a).toCharArray()[0] < list.get(b).toCharArray()[0]) { String temp = list.get(a); list.set(a, list.get(b)); list.set(b, temp); } } } } }