黑馬程序員——深入理解泛型

------- android培訓java培訓、期待與您交流! ----------

今天北京又下大雨,不小心把燈的開關弄掛了,修了半天沒搞利索,鬱悶,晚上放慢速度整理泛型問題,還是稍微有點問題,下午修理開關耗費太多精力和時間,導致胳膊有點抽筋,明天再解決問題。

package cn.heima2;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class GenericTest {
	public static void main(String[] args) throws Exception{
		Collection<Integer> collection = new ArrayList<Integer>();
		//雖然泛型定義的是存儲Integer類型,但是利用反射還是可以存入其他類型,很有用處,並且這一招很酷!
		collection.getClass().getMethod("add", Object.class).invoke(collection, "abc");
		System.out.println(((ArrayList<Integer>) collection).get(0));
		printCollection(collection);
		
		Collection<String> collection1 = new ArrayList<String>();
		Collection<Integer> collection2 = new ArrayList<Integer>();
		//雖然泛型不一樣但是兩個集合的字節碼是同一個
		System.out.println(collection1.getClass()==collection2.getClass());
		
		
		HashMap<String,Integer> hashMap = new HashMap<String,Integer>();
		hashMap.put("dandan", 22);
		hashMap.put("jiajia", 30);
		hashMap.put("qiqi", 25);
		Set<Map.Entry<String, Integer>> entryset = hashMap.entrySet();
		for(Map.Entry<String, Integer> entry:entryset){
			System.out.println(entry.getKey());
			System.out.println(entry.getValue());
		}
		String[] strs = {"abc","xyz","djq"};
		//只有引用類型才能作爲泛型方法的實際參數
		swap(strs,1,2);
		for(String str:strs){
			System.out.println(str);
		}
		fill_1(strs,"love");
		System.out.println("----------");
		for(String str:strs){
			System.out.println(str);
		}
		System.out.println("----------");
		fill_2(strs,"like");
		for(String str:strs){
			System.out.println(str);
		}
		copy_1(collection1,strs);
		System.out.println(collection1);
		String[] strs1=new String[strs.length];
		copy(strs1,collection1);
		System.out.println(Arrays.asList(strs1));
			
		

	}
	//定義一個方法,把任意參數類型的集合中的數據安全地複製到相應類型的數組中
	public static <T>void copy(T[]dest ,Collection<T> src){
		dest = (T[])src.toArray();
		System.out.println(Arrays.asList(dest));
		//src.toArray(dest);
		
	}
	//定義一個方法,把任意參數類型的數組中的數據安全地複製到相應類型的集合中
	public static <T>void copy_1(Collection<T> dest,T[] src){
		for(int i=0;i<src.length;i++){
			dest.add(src[i]);
		}
	}
	
	//定義一個方法,把任意參數類型的一個數組中的數據安全地複製到相應類型的數組中
     public static <T> void copy_2(T[] dest,T[] src){
    	 for(int i=0;i<src.length;i++){
    		 dest[i]=src[i];
    	 }
     }	
	//任意數組中的所有元素填充爲相應類型的對象
	//fill_1(),fill_2()區別從論壇上查詢了下,發現自己對增加for理解還是不夠透徹
	//增加for,就是用來遍歷的 ,不能改變值,增強for循環不能用於改變數組或集合的內容

	public static <T> void fill_1(T[]arrs,T t){
		for(T arr:arrs){
			arr=t;
			System.out.println(arr);
		}
	}
	public static <T> void fill_2(T[]arrs,T t){
		for(int i=0;i<arrs.length;i++){
			arrs[i]=t;
		}
	}
	
	//練習,自動將Object對象類型變爲其他類型
	private static <T>T autoConvert(Object obj){
		return (T)obj;
	}
	
	
	
	//兩個例子說明泛型的語法,加法和交換
	public static int add(int x,int y){
		return x+y;
	}
	public static <T>T add( T x,T y){
		return null;
	}
	
	public static <T>T swap(T[] arrs,int i,int j){
		T tmp = arrs[i];
		arrs[i]= arrs[j];
		arrs[j]=tmp;
		return null;
	}
	public static void printCollection(Collection<?> collection){
		System.out.println(collection.size());
		for(Object obj:collection){
			System.out.println(obj);
		}
	}
}


 

 ------- android培訓java培訓、期待與您交流! ----------  詳細請查看:http://edu.csdn.net/heima/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章