泛型集合(經典)

                                           泛型集合(經典)

 

1, 使用迭代器Iterator的方式。java

 

2, 使用加強for循環的方式。設計模式

 

3, 若是有下標,則可使用下標的方式。數組

 


 

(1)遍歷數組ide

 

 

 

[java] view plain copy
  1. String [] arr=new String[] {"aa","bb","cc"};  
  2.         //1.加強的for循環  
  3.         for(String elt:arr){  
  4.             System.out.println(elt);  
  5.         }  
  6.         //2.下標的方式  
  7.         for (int i = 0; i < arr.length; i++) {  
  8.             System.out.println(arr[i]);  
  9.         }  

 


(2)遍歷List集合函數

 

 

[java] view plain copy
  1. ArrayList<String> list=new ArrayList<String>();  
  2.         list.add("haha");  
  3.         list.add("hehe");  
  4.         list.add("xixi");  
  5.         //1.加強的for循環  
  6.         for(String elt:list){  
  7.             System.out.println(elt);  
  8.         }  
  9.         //2.下標的方式  
  10.         for (int i = 0; i < list.size(); i++) {  
  11.             System.out.println(list.get(i));  
  12.         }  
  13.         //3.迭代器的方式  
  14.         Iterator<String> it=list.iterator();  
  15.         while (it.hasNext()) {  
  16.             System.out.println(it.next());  
  17.               
  18.         }  



(3)遍歷Set集合
this

 

 

[java] view plain copy
  1. HashSet<String> set=new HashSet<String>();  
  2.         set.add("zhangsan");  
  3.         set.add("lisi");  
  4.         set.add("wangqu");  
  5.         //1.加強的for循環  
  6.         for(String elt:set){  
  7.             System.out.println(elt);  
  8.         }  
  9.         //2.迭代器  
  10.         Iterator<String> it=set.iterator();  
  11.         while (it.hasNext()) {  
  12.             System.out.println(it.next());  
  13.               
  14.         }  



(4)遍歷Map集合
spa

 

 

 

[java] view plain copy
    1. HashMap<Integer , String> map=new HashMap<Integer, String>();  
    2.         map.put(1, "alice");  
    3.         map.put(2, "bob");  
    4.         map.put(3, "lily");  
    5.         //1.加強的for循環(entry集合)  
    6.         for(Entry<Integer, String> entry:map.entrySet()){  
    7.             System.out.println(entry);  
    8.         }  
    9.         //2.加強的for循環(key集合)  
    10.         for(Integer key:map.keySet()){  
    11.             System.out.println(key+" = "+map.get(key));  
    12.         }  
    13.         //3.遍歷值的集合  
    14.         for(String value:map.values()){  
    15.             System.out.println(value);  
    16.         } 

       

    

 

泛型 的好處:
1. 把運行時出現 的問題提早至了編譯時。
2. 避免了無謂的強制類型轉換。

注意: 在泛型中沒有多態的概念,兩邊的數據必需要一致。 或者是隻寫一邊 的泛型類型。


.net

推薦使用: 兩邊的數據類型都寫上一致的。設計

泛型:對象

[java] view plain copy
  1. import java.util.ArrayList;  
  2.   
  3. public class demo1 {  
  4.     public static void main(String[] args) {  
  5.         ArrayList<String> list=new ArrayList<String>();  
  6.         list.add("aa");  
  7.         list.add("bb");  
  8.         //list.add(12);  
  9.         for (int i = 0; i < list.size(); i++) {  
  10.             String str=list.get(i);  
  11.             System.out.println(str.toUpperCase());  
  12.         }  
  13.     }  
  14. }  


泛型方法:

  當函數中使用了一個不明確的數據類型,那麼在函數上就能夠進行泛型的定義。

 

        public <泛型的聲明> 返回值類型 函數名( 泛型 變量名 ){

        

        

         }

注意:
1. 在方法上的自定義泛型的具體數據類型是調用該方法的時候傳入實參的時候肯定的。
2. 自定義泛型使用的標識符只要符合標識符的命名規則便可。

[java] view plain copy
  1. public class demo2 {  
  2.     public static void main(String[] args) {  
  3.         Integer i=print(12);  
  4.         String str=print("abc");  
  5.           
  6.   
  7.     }  
  8.     public static <T> T print(T o){  
  9.         return o;  
  10.     }  
  11. }  


泛型類:

泛型類的定義格式:

class 類名<聲明自定義的泛型>{




注意的事項: 
1. 在類上自定義的泛型的具體數據類型是在建立對象的時候指定的。
2. 在類上自定義了泛型,若是建立該類的對象時沒有指定泛型的具體類型,那麼默認是Object類型。

[java] view plain copy
  1. //自定義泛型類  
  2. class Father<T>{  
  3.     private T t;  
  4.     public Father() {  
  5.         // TODO Auto-generated constructor stub  
  6.     }  
  7.     public Father(T t){  
  8.         super();  
  9.         this.t=t;  
  10.     }  
  11.     public void setT(T t) {  
  12.         this.t = t;  
  13.     }  
  14.     public T getT() {  
  15.         return t;  
  16.     }  
  17. }  
  18. //泛型類的繼承方式1:子類也須要使用泛型  
  19. class Son1<T> extends Father<T>{  
  20.       
  21. }  
  22. //泛型類的繼承方式2:子類指定了具體的類型  
  23. class Son2 extends Father<String>{  
  24.       
  25. }  
  26. //錯誤寫法,父類上定義有泛型須要進行處理  
  27. /*class Son3 extends Father<T>{ 
  28.      
  29. }*/  
  30.   
  31. public class demo3 {  
  32.     public static void main(String[] args) {  
  33.           
  34.         //在類上自定義的泛型的具體數據類型是在建立對象的時候指定的。  
  35.         Father<String> f1=new Father<String>("jack");  
  36.         System.out.println(f1.getT());  
  37.         Father<Integer> f2=new Father<Integer>(20);  
  38.         System.out.println(f2.getT());  
  39.           
  40.           
  41.     }  
  42. }  


泛型接口:


泛型接口的定義格式:

interface 接口名<聲明自定義的泛型>{

}


在接口上自定義泛型要注意的事項:
1. 在接口上自定義泛型的具體數據類型是在實現該接口的時候指定的。
2. 若是一個接口自定義了泛型,在實現該接口的時候沒有指定具體的數據類型,那麼默認是Object數據類型。 

[java] view plain copy
  1. interface Inter<T>{  
  2.     void print(T t);  
  3. }  
  4. //實現不知爲什麼類型時能夠這樣定義  
  5. class myInter<T> implements Inter<T>{  
  6.     @Override  
  7.     public void print(T t) {  
  8.         // TODO Auto-generated method stub  
  9.         System.out.println("myprint:"+t);  
  10.     }  
  11. }  
  12. //使用接口時明確具體類型。  
  13. class myInter2 implements Inter<String>{  
  14.     @Override  
  15.     public void print(String t) {  
  16.         // TODO Auto-generated method stub  
  17.         System.out.println("myprint2:"+t);  
  18.     }  
  19. }  
  20. public class demo4 {  
  21.     public static void main(String[] args) {  
  22.         myInter<String> str=new myInter<String>();  
  23.         str.print("泛型");  
  24.         myInter2 str2=new myInter2();  
  25.         str2.print("只能傳字符串");  
  26.     }  
  27. }  



1、List遍歷

Java中List遍歷有三種方法來遍歷泛型,主要爲:

1.for循環遍歷

2.iterator遍歷

3.foreach遍歷

 

[java] view plain copy
  1. package com.gmail.lsgjzhuwei;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.Iterator;  
  5. import java.util.List;  
  6.   
  7. import org.junit.Test;  
  8.   
  9. public class test {  
  10.   
  11.     //第一種方法:for循環遍歷  
  12.     @Test  
  13.     public void test1() {  
  14.         List<String> li = new ArrayList<String>();  
  15.   
  16.         li.add("agad");  
  17.         li.add("1234");  
  18.         li.add("good");  
  19.   
  20.         for (int i = 0; i < li.size(); i++) {  
  21.             String s = li.get(i);  
  22.             System.out.println(s);  
  23.         }  
  24.         System.out.println("-------------------");  
  25.     }  
  26.   
  27.     //第二種方法:iterator遍歷  
  28.     @Test  
  29.     public void test2() {  
  30.         List<String> li = new ArrayList<String>();  
  31.   
  32.         li.add("agad");  
  33.         li.add("1234");  
  34.         li.add("good");  
  35.   
  36.         Iterator iterator = li.iterator();  
  37.         while (iterator.hasNext()) {  
  38.             String s = (String) iterator.next();  
  39.             System.out.println(s);  
  40.         }  
  41.         System.out.println("-------------------");  
  42.     }  
  43.   
  44.     //第三種方法:foreach方法遍歷  
  45.     @Test  
  46.     public void test3() {  
  47.         List<String> li = new ArrayList<String>();  
  48.   
  49.         li.add("agad");  
  50.         li.add("1234");  
  51.         li.add("good");  
  52.   
  53.         foreach (String s : li) {  
  54.             System.out.println(s);  
  55.         }  
  56.   
  57.         System.out.println("-------------------");  
  58.     }  
  59. }  

 

2、Map遍歷

Map遍歷只要有兩種方法: 

1.經過Map的KeySet進行遍歷

2.經過Map的EntrySet進行遍歷

 

[java] view plain copy
    1. // Map的遍歷方法一:經過map的KeySet進行遍歷  
    2.     @Test  
    3.     public void test4() {  
    4.         Map<Integer, String> map = new HashMap<Integer, String>();  
    5.         map.put(1, "good");  
    6.         map.put(2, "morning");  
    7.   
    8.         Set<Integer> set = map.keySet();  
    9.         for (Integer ky : set) {  
    10.             System.out.println(ky + ":" + map.get(ky));  
    11.         }  
    12.   
    13.         System.out.println("-------------------");  
    14.     }  
    15.   
    16.     // Map的遍歷方法二:經過map的entrySet進行遍歷  
    17.     @Test  
    18.     public void test5() {  
    19.         Map<Integer, String> map = new HashMap<Integer, String>();  
    20.         map.put(1, "good");  
    21.         map.put(2, "morning");  
    22.   
    23.         Set<Map.Entry<Integer, String>> set = map.entrySet();  
    24.         for (Entry<Integer, String> entry : set) {  
    25.             System.out.println(entry.getKey() + ":" + entry.getValue());  
    26.         }  
    27.   
    28.         System.out.println("-------------------");  
    29.     } 

       

       

單例設計模式分爲懶人模式和餓人模式,若是一個枚舉中只有一個用例,它就至關於一個單例設計模式

[java] view plain copy
  1. // 餓漢  

  2. class B {  

  3.     // 一、私有構造器  

  4.     private B() {  

  5.     }  

  6.     // 二、private static 對象成員  

  7.     private static B b = new B();  

  8.     // 三、提供public static 獲取成員方法,獲取惟一實例  

  9.     public static B getInstance() {  

  10.         return b;  

  11.     }  

  12. }  

  13.   

  14. //懶漢  

  15. class C {  

  16.     // 一、私有構造器  

  17.     private C() {  

  18.     }  

  19.     // 二、private static 對象成員  

  20.     private static C c;  

  21.     // 三、提供public static 獲取成員方法,獲取惟一實例  

  22.     public static C getInstance() {  

  23.         if(c == null){  

  24.             c = new C();  //懶漢式  

  25.         }  

  26.         return c;  

  27.     }  

  28. }  

相關文章
相關標籤/搜索