Java不可不知的泛型使用示例代码
本文介绍了Java的泛型的基本使用。
1.为什么使用泛型
看下面一个例子:
为了说明问题,本类写的尽量简陋,请把目光主要放在类型上。
/** *@authorXingXiaoguan(xingrenguanxue) */ publicclassMyArrayList{ privateint[]elementData; privateintsize=0; publicMyArrayList(intcapacity){ elementData=newint[capacity]; } //向数组中添加元素 publicvoidadd(inti){ if(size==elementData.length){ thrownewIndexOutOfBoundsException("数组已满"); } elementData[size++]=i; } //从数组中根据下标获取元素 publicintget(intindex){ if(index<0||index>size-1){ thrownewIndexOutOfBoundsException("超出范围"); } returnelementData[index]; } @Override publicStringtoString(){ return"MyArrayList{"+ "elementData="+Arrays.toString(elementData)+ '}'; } }
该类很简单:有两个成员变量,elementData是一个数组,size是数组中元素的数量。add和get方法能添加和获取元素。
下面测试一下:
publicclassTest{ publicstaticvoidmain(String[]args){ MyArrayListmyArrayList=newMyArrayList(4); myArrayList.add(111);//向数组中添加3个int元素 myArrayList.add(222); myArrayList.add(333); inti=myArrayList.get(0);//获取 System.out.println(i); //以上正常运行 myArrayList.add("行小观");//添加一个String元素,类型不匹配,报错 } }
向数组中添加3个int类型的元素并能获取,这没问题。
但是如果我们的场景不再需要int类型的元素,而是需要String类型的,那怎么办?
很显然,继续使用该类会报错,报错的原因很简单:我们向数组中添加的元素是String类型的,而数组和方法参数类型是int类型。
此时,就得需要再写一份代码,该份代码较之前的并无大修改,只是把int改为String。如果场景继续变怎么办?那就再写一份新代码!
这样太麻烦了!有没有解决办法?有!
我们知道,Object类是所有类的父类,Object类型的变量能够引用任何类型的对象。所以可以将类型改为Object:
/** *@authorXingXiaoguan(xingrenguanxue) */ publicclassMyArrayList{ privateObject[]elementData; privateintsize=0; publicMyArrayList(intcapacity){ elementData=newObject[capacity]; } publicvoidadd(Objecto){//向数组中添加元素 if(size==elementData.length){ thrownewIndexOutOfBoundsException("数组已满"); } elementData[size++]=o; } publicObjectget(intindex){//从数组中获取元素 if(index<0||index>size-1){ thrownewIndexOutOfBoundsException("超出范围"); } returnelementData[index]; } @Override publicStringtoString(){ return"MyArrayList{"+ "elementData="+Arrays.toString(elementData)+ '}'; } }
再测试一下:
publicclassTest{ publicstaticvoidmain(String[]args){ //myArrayList给int元素使用 MyArrayListmyArrayList=newMyArrayList(4); myArrayList.add(111);//向数组中添加3个int元素 myArrayList.add(222); myArrayList.add(333); inti=(int)myArrayList.get(0);//获取 System.out.println(i); //myArrayList给String元素使用 MyArrayListmyArrayList1=newMyArrayList(4); myArrayList1.add("aaa"); myArrayList1.add("bbb"); myArrayList1.add("ccc"); Stringstr=(String)myArrayList1.get(1); System.out.println(str); } }
发现可以向数组中添加和获取int或String类型的元素,这证明该类的数组和方法同时对各种类型的数据都有用,不必再添加额外代码。
但是这样又出现了两个问题:
第一:从数组中获取元素时,需要强制转换类型才行。
inti=(int)myArrayList.get(0);
第二:同一个数组可以添加各种类型的元素。
myArrayList.add(111);//int类型 myArrayList.add("222");//String类型 myArrayList.add(true);//布尔类型
这就导致了当我们从数组中获取某个元素时,很难知道它的确切类型,往往会强转类型失败。
inti=(int)myArrayList.get(1);//本来是String类型的值,但我提前不知道,拿int变量接收,报错
那这个问题有没有解决办法呢?
有!用泛型!
2.泛型类
使用泛型改造MyArrayList:
/** *@authorXingXiaoguan(xingrenguanxue) */ publicclassMyArrayList{ privateT[]elementData; privateintsize=0; publicMyArrayList(intcapacity){ elementData=(T[])newObject[capacity]; } publicvoidadd(To){//向数组中添加元素 if(size==elementData.length){ thrownewIndexOutOfBoundsException("数组已满"); } elementData[size++]=o; } publicTget(intindex){//从数组中获取元素 if(index<0||index>size-1){ thrownewIndexOutOfBoundsException("超出范围"); } returnelementData[index]; } @Override publicStringtoString(){ return"MyArrayList{"+ "elementData="+Arrays.toString(elementData)+ '}'; } }
测试:
publicclassTest{ publicstaticvoidmain(String[]args){ //myArrayList给int元素使用 MyArrayListmyArrayList=newMyArrayList<>(4); myArrayList.add(111);//向数组中添加3个int元素 //myArrayList.add("222");//添加非Integer元素报错 inti=myArrayList.get(1);//无需强制转型 System.out.println(i); } }
经过改造,我们把MyArrayList类改为了一个泛型类,它是一个具有多个类型变量的类。
泛型类的声明方式:引入一个类型变量,如T,然后使用<>将其括起来,放在类名后。
publicclassMyArrayList{ //...... }
如何理解类型变量?它就类似于数学中函数中的变量x,用来代替具体的值:
f(x)=3x+1
类型变量的名称可以随便取,一般使用大写字母表示,比如E、K、V、T等。
泛型类中的成员变量、方法参数和返回值的类型都使用类型变量代替:
privateT[]elementData; publicvoidadd(To){ //....... } publicTget(intindex){ //...... }
当然,一个泛型类可以有多个类型变量:
publicclassMyClass{ //...... }
当我们需要实例化泛型类时,就使用具体的类型来替换类型变量(T):
MyArrayListmyArrayList=newMyArrayList<>(4);
该过程就相当于向数学函数中代入数值:
f(3)=3*3+1=10
3.泛型方法
当我们声明了一个泛型类后,可以很自然地在类内部使用泛型方法。
其实,当类是普通类时,我们仍然能够使用泛型方法。下面是一个例子:
/** *@authorXingXiaoguan(xingrenguanxue) */ publicclassPrinterVar{ //该方法接收一个T类型的变量,打印并返回该变量 publicTprint(Tvar){ System.out.println(var); returnvar; } publicstaticvoidmain(String[]args){ PrinterVarprinterVar=newPrinterVar(); Stringvar=printerVar.print("aa");//String类型 Integervar1=printerVar.print(12);//int类型 System.out.println(var+""+var1); } }
到此这篇关于Java不可不知的泛型使用的文章就介绍到这了,更多相关Java泛型使用内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!