天天看點

【JavaSE學習筆記】泛型,jdk5之後新特性

泛型,jdk5之後新特性

A.泛型
1)概述
建立集合對象或者去調用方法的時候,将資料類型(引用類型)當作一種參數進行傳遞
2)格式
<資料類型>:引用類型
3)特點

a.将運作時期的異常提前到了編譯期間

b.不用再強制轉換類型

c.解決了黃色警告線的問題

注意:在sun公司,jdk官方文檔中,凡是類後面,接口後面,抽象類後面帶有<E>:泛型定義

import java.util.ArrayList;
import java.util.Iterator;

public class Demo01 {
	public static void main(String[] args) {
		// 建立ArrayList集合對象
		ArrayList<String> al = new ArrayList<String>();// ----------->jdk7以後的特性:叫泛型推斷,

		// 給集合中添加元素
		al.add("hello");
		al.add("world");
		al.add("java");

		// 給集合中添加一個Integer類型的元素
		// array.add(new Integer(100)) ;

		// 将運作時期異常提前到編譯期間
		// array.add(100);//沒有報錯的原因是什麼呢?自動裝箱:int--->Integer類型

		// 擷取疊代器對象
		Iterator<String> i = al.iterator();
		while (i.hasNext()) {
			String s = i.next();
			System.out.println(s);
		}

	}
}
           
4)使用泛型來存儲自定義對象,并且周遊該對象的成員!
首先建一個學生類,跟之前一樣,含有name,age成員變量,在此不再介紹,直接調用
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

public class Demo02 {
	public static void main(String[] args) {
		// 建立集合對象
		ArrayList<Student> als = new ArrayList<Student>();// 前後保持一緻
		
		//建立4個學生對象
		Student s1 = new Student("Tom", 20);
		Student s2 = new Student("Tim", 21);
		Student s3 = new Student("Tommon", 22);
		Student s4 = new Student("Timmon", 23);
		
		//給集合中添加對象
		als.add(s1);
		als.add(s2);
		als.add(s3);
		als.add(s4);
		
		//方式1 普通for循環
		for (int i = 0; i < als.size(); i++) {
			Student s = als.get(i);
			System.out.println(s);
		}
		System.out.println("--------------------------------");
		
		//方式2 Iterator iterator()
		Iterator<Student> its = als.iterator();
		while (its.hasNext()) {
			Student s = its.next();
			System.out.println(s);
		}
		System.out.println("--------------------------------");
		
		//方式3 清單疊代器
		ListIterator<Student> lls = als.listIterator();
		while (lls.hasNext()) {
			Student s = lls.next();
			System.out.println(s);
		}
	}
}
           
5)泛型的應用場景

定義在類(具體類或者抽象類),接口的後面,一般情況下,在集合中使用居多

接口

public class ObjectTool<T> {// T代表所有類
	private T obj;

	public T getObj() {
		return obj;
	}

	public void setObj(T obj) {
		this.obj = obj;
	}

}
           
//測試類
//測試類
public class Demo03 {
	public static void main(String[] args) {
		// 建立工具類對象:已經将資料類型給定了
		ObjectTool<String> ots = new ObjectTool<String>();

		// ots.setObj(new Integer(29));使用泛型提高了程式的安全性
		ots.setObj(new String("Tom"));
		String s = ots.getObj();
		System.out.println("姓名是:" + s);

		ObjectTool<Integer> oti = new ObjectTool<Integer>();
		oti.setObj(new Integer(27));
		Integer i = oti.getObj();
		System.out.println("年齡是:" + i);
	}
}
           
6)泛型可以定義在方法上嗎?
public class ObjectTool {
	public <T> void show(T t) {// 泛型定義在方法上:格式:public <T> void 方法名 (重點)
		System.out.println(t);
	}
}
           
給類後面沒有添加泛型也可以操作,需要泛型定義在方法上
//測試類
public class Demo03 {
	public static void main(String[] args) {
		ObjectTool ot = new ObjectTool();
		ot.show("hello");
		ot.show(true);
		ot.show(100);
	}
}
           
7)将泛型定義在接口上
接口(也可以把泛型定義在抽象方法上:abstract後面。。自行測試)
//接口:将泛型定義在接口上
public interface Inter<T> {
	public abstract void show(T t);
}
           
子實作類
//第二種情況:不明确資料類型不明确接口的資料類型是什麼
public class InterImpl<T> implements Inter<T> {

	@Override
	public void show(T t) {
		System.out.println(t);
	}
}
           
測試類
//測試類
public class Demo04 {
	public static void main(String[] args) {
		Inter<String> is = new InterImpl<String>();
		is.show("Tom");
		
		Inter<Integer> ii = new InterImpl<Integer>();
		ii.show(29);
	}
}
           
8)泛型進階:通配符

?:代表任意資料類型:可以是Object類型以及Java類

? extends E:向下限定,要麼跟目前類型一樣,要麼是它的子類

? super E:向上限定,保持目前類型一緻,或者是它的父類

import java.util.ArrayList;
import java.util.Collection;

class Animal{}

class Dog extends Animal{}

class Cat extends Animal{}

public class Demo05 {
	public static void main(String[] args) {
		Collection<Object> c1 = new ArrayList<Object>();
		Collection<Animal> c2 = new ArrayList<Animal>();
		
		//?
		Collection<?> c3 = new ArrayList<Object>();
		Collection<?> c4 = new ArrayList<Animal>();
		Collection<?> c5 = new ArrayList<Dog>();
		Collection<?> c6 = new ArrayList<Cat>();
		
		//? extends E
		Collection<? extends Object> c7 = new ArrayList<Animal>();
		Collection<? extends Animal> c8 = new ArrayList<Animal>();
		Collection<? extends Animal> c9 = new ArrayList<Cat>();
		Collection<? extends Animal> c10 = new ArrayList<Dog>();
		
		//? super E
		Collection<? super Animal> c11 = new ArrayList<Object>();
		Collection<? super Cat> c12 = new ArrayList<Animal>();
	}
}
           
9)集合的嵌套

我們有一個班裡,班裡有很多學生,周遊學生的資訊

我們隔壁班也有學生----->ArrayList<Student>.有很多班,最終的大的集合

ArrayList<ArrayList<Student>>------->自定義學生類

然後實作集合的嵌套周遊(增強for循環,B節介紹):使用ArrayList

import java.util.ArrayList;

public class Demo06 {
	public static void main(String[] args) {
		// 定義一個大集合對象
		ArrayList<ArrayList<Student>> aals = new ArrayList<ArrayList<Student>>();

		// 建立第一個班集合對象
		ArrayList<Student> als1 = new ArrayList<Student>();

		// 建立學生對象
		Student s1 = new Student("劉備", 29);
		Student s2 = new Student("關羽", 28);
		Student s3 = new Student("張飛", 27);

		// 給集合添加對象
		als1.add(s1);
		als1.add(s2);
		als1.add(s3);

		// 将第一個集合對象添加到大集合中
		aals.add(als1);

		// 建立第二個班集合對象
		ArrayList<Student> als2 = new ArrayList<Student>();

		// 建立學生對象
		Student s4 = new Student("曹操", 29);
		Student s5 = new Student("許褚", 28);
		Student s6 = new Student("張遼", 27);

		// 給集合添加對象
		als2.add(s4);
		als2.add(s5);
		als2.add(s6);

		// 将第二個集合對象添加到大集合中
		aals.add(als2);

		// 建立第三個班集合對象
		ArrayList<Student> als3 = new ArrayList<Student>();

		// 建立學生對象
		Student s7 = new Student("孫權", 29);
		Student s8 = new Student("周瑜", 28);
		Student s9 = new Student("陸遜", 27);

		// 給集合添加對象
		als3.add(s7);
		als3.add(s8);
		als3.add(s9);

		// 将第三個集合對象添加到大集合中
		aals.add(als3);

		// 使用增強for循環
		for (ArrayList<Student> arrayList : aals) {
			for (Student s : arrayList) {
				System.out.println(s);
			}
			System.out.println("----------------------------");
		}
	}
}
           
B.jdk5之後新特性
1)概述
jdk5以後的新特性:自動拆裝箱,泛型,枚舉,增強for循環,靜态導入,可變參數等等
2)增強for循環

是用來将數組和集合的周遊簡單化

a.格式:(集合中用的比較多)

for(資料類型(引用類型) 變量名 : 數組或者集合的對象名稱){
輸出變量名;
}

b.應用場景:不是集合就是數組(int類型(建議寫Integer)/String類型的居多)

c.弊端:該對象不能為空,否則報錯:NullPointerException:空指針異常

d.為什麼說增強for循環相當于疊代器?

上一章中,判斷如果字元串元素是一個world元素

給集合中添加一個新的元素:javaee

在增強for中,會發生并發異常

3)使用增強for循環,周遊字元串
import java.util.ArrayList;

public class Demo01 {
	public static void main(String[] args) {
		ArrayList<String> als = new ArrayList<String>();

		als.add("hello");
		als.add("world");
		als.add("java");

		for (String s : als) {
			System.out.println(s);
		}
	}
}
           
4)集合存儲自定義對象并周遊
import java.util.ArrayList;

public class Demo02 {
	public static void main(String[] args) {
		ArrayList<Student> als = new ArrayList<Student>();
		
		Student s1 = new Student("劉備", 29);
		Student s2 = new Student("關羽", 28);
		Student s3 = new Student("張飛", 27);
		Student s4 = new Student("趙雲", 26);
		
		als.add(s1);
		als.add(s2);
		als.add(s3);
		als.add(s4);
		
		for (Student s : als) {
			System.out.println(s);
		}
	}
}
           
5)靜态導入

a.格式:import static java.lang.Math.abs;

b.注意:1.一類中有靜态方法,就可以使用靜态導入

              2.如果要使用靜态導入,為了防止在一個類中出現同名的方法

                那麼調用的時候需要字首來差別開來

import static java.lang.Math.abs;
import static java.lang.Math.pow;
import static java.lang.Math.sqrt;


public class Demo03 {
	public static void main(String[] args) {
//		System.out.println(abs(-10));
		System.out.println(java.lang.Math.abs(-100));
		System.out.println(pow(2, 3));
		System.out.println(sqrt(4));
	}
	
	public static void abs(int i) {
		System.out.println(i);
	}
}
           
6)可變參數

a.格式:修飾符 傳回值類型 方法名(參數類型...變量名){}

b.多個參數其實相當于由資料組成的一個資料

c.如果一個方法中有多個參數,那麼可變參數指定的最後一個參數

求資料之和

public class Demo04 {
	public static void main(String[] args) {
		int sum = sum(10,20,30,40,50);
		System.out.println(sum);
	}
	
	public static int sum(int...a) {
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum += a[i];
		}
		return sum;
	}
}
           
7)數組轉換成集合
Arrays數組工具類

public static <T> List<T> asList(T...a):将數組轉換成集合

注意:數組可以轉換成集合,但是集合長度不能改變

import java.util.Arrays;
import java.util.List;

public class Demo05 {
	public static void main(String[] args) {
		String[] strArray = { "hello", "world", "java" };

		List<String> asList = Arrays.asList(strArray);

		// 給集合中添加一個元素
		// asList.add("javaee");java.lang.UnsupportedOperationException
		// asList.remove(1);

		for (String s : asList) {
			System.out.println(s);
		}

		// 修改集合中的元素
		asList.set(1, "word");
		System.out.println(asList);
	}
}