前面的文章:
- 詳解Java的物件創建
- 一文打盡Java繼承的相關問題
- 一文打盡Java抽象類和介面的相關問題
本文介紹了Java的泛型的基本使用,
1. 為什么使用泛型
看下面一個例子:
為了說明問題,本類寫的盡量簡陋,請把目光主要放在型別上,
/**
* @author Xing Xiaoguan (xingrenguanxue)
*/
public class MyArrayList {
private int[] elementData;
private int size = 0;
public MyArrayList(int capacity) {
elementData = https://www.cnblogs.com/xingrenguanxue/p/new int[capacity];
}
//向陣列中添加元素
public void add(int i) {
if (size == elementData.length) {
throw new IndexOutOfBoundsException("陣列已滿");
}
elementData[size++] = i;
}
//從陣列中根據下標獲取元素
public int get(int index) {
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("超出范圍");
}
return elementData[index];
}
@Override
public String toString() {
return "MyArrayList{" +
"elementData="https://www.cnblogs.com/xingrenguanxue/p/+ Arrays.toString(elementData) +'}';
}
}
該類很簡單:有兩個成員變數,elementData是一個陣列,size是陣列中元素的數量,add和get方法能添加和獲取元素,
下面測驗一下:
public class Test {
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList(4);
myArrayList.add(111); //向陣列中添加3個int元素
myArrayList.add(222);
myArrayList.add(333);
int i = myArrayList.get(0); //獲取
System.out.println(i);
//以上正常運行
myArrayList.add("行小觀"); //添加一個String元素,型別不匹配,報錯
}
}
向陣列中添加3個int型別的元素并能獲取,這沒問題,
但是如果我們的場景不再需要int型別的元素,而是需要String型別的,那怎么辦?
很顯然,繼續使用該類會報錯,報錯的原因很簡單:我們向陣列中添加的元素是String型別的,而陣列和方法引數型別是int型別,
此時,就得需要再寫一份代碼,該份代碼較之前的并無大修改,只是把int改為String,如果場景繼續變怎么辦?那就再寫一份新代碼!
這樣太麻煩了!有沒有解決辦法?有!
我們知道,Object類是所有類的父類,Object型別的變數能夠參考任何型別的物件,所以可以將型別改為Object:
/**
* @author Xing Xiaoguan (xingrenguanxue)
*/
public class MyArrayList {
private Object[] elementData;
private int size = 0;
public MyArrayList(int capacity) {
elementData = https://www.cnblogs.com/xingrenguanxue/p/new Object[capacity];
}
public void add(Object o) { //向陣列中添加元素
if (size == elementData.length) {
throw new IndexOutOfBoundsException("陣列已滿");
}
elementData[size++] = o;
}
public Object get(int index) { //從陣列中獲取元素
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("超出范圍");
}
return elementData[index];
}
@Override
public String toString() {
return "MyArrayList{" +
"elementData="https://www.cnblogs.com/xingrenguanxue/p/+ Arrays.toString(elementData) +'}';
}
}
再測驗一下:
public class Test {
public static void main(String[] args) {
//myArrayList 給int元素使用
MyArrayList myArrayList = new MyArrayList(4);
myArrayList.add(111); //向陣列中添加3個int元素
myArrayList.add(222);
myArrayList.add(333);
int i = (int) myArrayList.get(0); //獲取
System.out.println(i);
//myArrayList 給String元素使用
MyArrayList myArrayList1 = new MyArrayList(4);
myArrayList1.add("aaa");
myArrayList1.add("bbb");
myArrayList1.add("ccc");
String str = (String) myArrayList1.get(1);
System.out.println(str);
}
}
發現可以向陣列中添加和獲取int或String型別的元素,這證明該類的陣列和方法同時對各種型別的資料都有用,不必再添加額外代碼,
但是這樣又出現了兩個問題:
第一:從陣列中獲取元素時,需要強制轉換型別才行,
int i = (int) myArrayList.get(0);
第二:同一個陣列可以添加各種型別的元素,
myArrayList.add(111); //int型別
myArrayList.add("222"); //String型別
myArrayList.add(true); //布爾型別
這就導致了當我們從陣列中獲取某個元素時,很難知道它的確切型別,往往會強轉型別失敗,
int i = (int)myArrayList.get(1); //本來是String型別的值,但我提前不知道,拿int變數接收,報錯
那這個問題有沒有解決辦法呢?
有!用泛型!
2. 泛型類
使用泛型改造MyArrayList:
/**
* @author Xing Xiaoguan (xingrenguanxue)
*/
public class MyArrayList <T> {
private T[] elementData;
private int size = 0;
public MyArrayList(int capacity) {
elementData = https://www.cnblogs.com/xingrenguanxue/p/(T[]) new Object[capacity];
}
public void add(T o) { //向陣列中添加元素
if (size == elementData.length) {
throw new IndexOutOfBoundsException("陣列已滿");
}
elementData[size++] = o;
}
public T get(int index) { //從陣列中獲取元素
if (index < 0 || index > size - 1) {
throw new IndexOutOfBoundsException("超出范圍");
}
return elementData[index];
}
@Override
public String toString() {
return "MyArrayList{" +
"elementData="https://www.cnblogs.com/xingrenguanxue/p/+ Arrays.toString(elementData) +'}';
}
}
測驗:
public class Test {
public static void main(String[] args) {
//myArrayList 給int元素使用
MyArrayList<Integer> myArrayList = new MyArrayList<>(4);
myArrayList.add(111); //向陣列中添加3個int元素
// myArrayList.add("222"); //添加非Integer元素報錯
int i = myArrayList.get(1); //無需強制轉型
System.out.println(i);
}
}
經過改造,我們把MyArrayList類改為了一個泛型類,它是一個具有多個型別變數的類,
泛型類的宣告方式:引入一個型別變數,如T,然后使用<>將其括起來,放在類名后,
public class MyArrayList <T> {
//......
}
如何理解型別變數?它就類似于數學中函式中的變數x,用來代替具體的值:
f(x) = 3x + 1
型別變數的名稱可以隨便取,一般使用大寫字母表示,比如E、K、V、T等,
泛型類中的成員變數、方法引數和回傳值的型別都使用型別變數代替:
private T[] elementData;
public void add(T o) {
//.......
}
public T get(int index) {
//......
}
當然,一個泛型類可以有多個型別變數:
public class MyClass <K, V> {
//......
}
當我們需要實體化泛型類時,就使用具體的型別來替換型別變數(T):
MyArrayList<Integer> myArrayList = new MyArrayList<>(4);
該程序就相當于向數學函式中代入數值:
f(3) = 3*3+1 = 10
3. 泛型方法
當我們宣告了一個泛型類后,可以很自然地在類內部使用泛型方法,
其實,當類是普通類時,我們仍然能夠使用泛型方法,下面是一個例子:
/**
* @author Xing Xiaoguan (xingrenguanxue)
*/
public class PrinterVar {
//該方法接收一個T型別的變數,列印并回傳該變數
public <T> T print(T var) {
System.out.println(var);
return var;
}
public static void main(String[] args) {
PrinterVar printerVar = new PrinterVar();
String var = printerVar.print("aa");//String型別
Integer var1 = printerVar.print(12);//int型別
System.out.println(var + " " + var1);
}
}
4. 關于我
點擊這里認識我 , (^o^)/
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/96342.html
標籤:Java
下一篇:Java面試題——陣列求和統計
