1. 程式人生 > >看視訊自學Java泛型

看視訊自學Java泛型

1.泛型

程式碼:

/**
 * 泛型
 * @author Boom
 *
 */
public class Demo1 {
	
	public static void main(String[] args) {
		List list = new ArrayList();
		list.add("aaa");
		Integer i = (Integer) list.get(0);
		
		//區別,使用泛型避免轉換錯誤
		List<String> list1 =new ArrayList<String>();
		list1.add("aaaa");
		list1.get(0);
	}
}

=============================================================

2.用傳統方法和for的增強式遍歷使用泛型的集合或陣列 

    低階程式設計師使用高階程式設計師的程式碼

程式碼:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hamcrest.core.Is;
import org.junit.Test;
/**
 * 泛型
 * @author Boom
 *
 */
public class Demo2 {
	
	@Test
	public void test1(){
		
		List<String> list =new ArrayList<String>();
		list.add("aa");
		list.add("bb");
		list.add("cc");
		
		//傳統
		Iterator it = list.iterator();
		while(it.hasNext()){
			String value =(String) it.next();
			System.out.println(value);
		}
		//增強for
		for(String s : list){
			System.out.println(s);
		}
	}
	
	@Test
	public void test2(){
		
		//模擬資料
		Map<Integer, String> map = new LinkedHashMap<Integer,String>(); 
		map.put(1, "aaa");
		map.put(2, "bbb");
		map.put(3, "ccc");
		
		//傳統 keyset 或者 entryset
		Set<Map.Entry<Integer, String>> set = map.entrySet();
		Iterator<Map.Entry<Integer, String>> it = set.iterator();
		while(it.hasNext()){
			Map.Entry<Integer, String> entry = it.next();
			int key =entry.getKey();
			String value =entry.getValue();
			System.out.println(key+"="+value);
		}
	
		//增強for
		for(Map.Entry<Integer, String> entry:map.entrySet()){
			int key =entry.getKey();
			String value =entry.getValue();
			System.out.println(key+"="+value);
		}
		
	}
	
	/**
	 * 泛型,低階程式設計師  能 使用高階程式設計師程式碼
	 */
	
	@Test
	public void test3(){
		//用泛型時,如果兩天都使用到泛型,兩邊必須要一樣
		
		// ArrayList<Object> list = new  ArrayList<String>();
		// ArrayList<String> list = new  ArrayList<String>();
		
		//相當於低階程式設計師能使用高階程式設計師程式碼
		ArrayList<String> list = new  ArrayList();
		ArrayList list1 = new  ArrayList<String>();
	}
}

=============================================================

3.泛型的擦除 (就是簡寫)
/**
 * 泛型的擦除
 * @author Boom
 *
 */
public class Demo3 {
	
	public static void main(String[] args) {
		ArrayList<String> list = new ArrayList<String>();
	}
}
/**
 * 泛型的擦除
 * @author Boom
 *
 */
public class Demo4 {
	
	public static void main(String[] args) {
		ArrayList list = new ArrayList();
	}
}

===============================================================

4.自定義泛型方法 (<T>是對引數的宣告)

程式碼:

/**
 * 自定義泛型方法
 * @author Boom
 * @param <V>
 *
 */
public class Demo5 {
	
	public void testa(){
		a("aaaa");
	}
	
	//<T> 是宣告
	public <T> T a(T t){
		return null;
	}
	
	//可以定義多個泛型
	public <T,E,K,V> void b(T t,E e,K k,V v){
		
	}
}

=================================================================

5.自定義泛型--泛型類和反射泛型  (其實,就是在4的基礎上把方法前的宣告遷移到類上)
/**
 * 自定義泛型--泛型類和反射泛型
 * @author Boom
 * @param <V>
 *
 */
//可以把方法上的宣告放到類上
public class Demo6<T,E,K,V> {
	
	public void testa(){
		//a("aaaa");
	}
	
	public T a(T t){
		return null;
	}
	
	
	public  void b(T t,E e,K k,V v){
		
	}
	
	//靜態方法
	public static <T> void c(T t){
		
	}
	
}

=================================================================

6.用泛型自定義個數組內元素互換的泛型方法
public class Demo7 {

	// 編寫一個泛型方法,實現指定位置上的陣列元素的交換
	public <T> void swap(T arr[], int pos1, int pos2) {
		T temp = arr[pos1];
		arr[pos1] = arr[pos2];
		arr[pos2] = temp;
	}

	// 編寫一個泛型方法,接收一個任意陣列,並顛倒陣列中的所有元素
	public <T> void reverse(T arr[]) {
		int start = 0;
		int end = arr.length - 1;
		while (true) {
			if (start >= end) {
				break;
			}
			T temp = arr[start];
			arr[start] = arr[end];
			arr[end] = temp;
			start++;
			end--;
		}
	}
}


總結:這個在架構方面用的比較多,就相當於低階程式設計師用高階程式設計師的程式碼。