認(rèn)識泛型
package cn.ArrayListDemo.com;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 泛型在哪些地方使用呢礁扮?
* 如果類忿偷,接口蛉威,抽象類后面跟有<E>就說要使用泛型材蛛,一般來說就是在集合中使用
*/
public class ArrayListDemo {
public static void main(String[] args) {
//用ArrayList存儲字符串元素圆到,并遍歷,用泛型改進(jìn)代碼
ArrayList<String> arrayList=new ArrayList<String>();
//添加元素
arrayList.add("hello");
arrayList.add("world");
arrayList.add("java");
//遍歷一
Iterator<String> iterator=arrayList.iterator();
while (iterator.hasNext()) {
String s1 = iterator.next();
System.out.println(s1);
}
System.out.println("----------");
//遍歷二
for (int x = 0; x < arrayList.size(); x++) {
String s2=arrayList.get(x);
System.out.println(s2);
}
}
}
之前我們在寫代碼的時候都會提示<E>,有這種的類或者接口卑吭,都是泛型芽淡;
- 存儲自定義對象并遍歷;
package cn.ArrayListDemo.com;
import java.util.ArrayList;
import java.util.Iterator;
import cn.manman.com.Student;
public class ArrayListDemo1 {
public static void main(String[] args) {
//創(chuàng)建集合對象
ArrayList<Student> arrayList=new ArrayList<Student>();
//創(chuàng)建集合對象
Student student1=new Student("李四",1);
Student student2=new Student("王五",2);
Student student3=new Student("張三",3);
//添加元素
arrayList.add(student1);
arrayList.add(student2);
arrayList.add(student3);
//遍歷
Iterator<Student> it=arrayList.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName()+"-->"+s.getAge());
}
//遍歷二
for (int x = 0; x < arrayList.size(); x++) {
Student s1 = arrayList.get(x);
System.out.println(s1.getName()+"-->"+s1.getAge());
}
}
}
泛型的應(yīng)用
- 泛型類
1豆赏、把泛型定義在類上
2挣菲、格式:public class 類名<泛型類型1,....>
3河绽、注意:泛型類型必須是引用類型
package cn.itcast_01;
public class ObjectTool<T> {
public void show(T t){
System.out.println(t);
}
}
前面的案例都是將泛型定義在類上己单,所以比較簡單的;
- 泛型方法
1耙饰、把泛型定義在方法上
2纹笼、格式:public <泛型類型> 返回類型 方法名(泛型類型...)
package cn.itcast_02;
/*
* 泛型定義在方法上
* 好處是:方法可以接收任意類型
*/
public class ObjectTool {
public <T> void show(T t) {
System.out.println(t);
}
}
package cn.itcast_02;
/*
* 測試類
*/
public class ObjectToolDemo {
public static void main(String[] args) {
ObjectTool ot=new ObjectTool();
ot.show("張三");
ot.show(100);
ot.show(true);
//System.out.println(ot);
}
}
- 泛型接口
package cn.itcast_03;
/*
* 泛型接口
*/
public interface Inter<T> {
public abstract void show(T t);
}
package cn.itcast_03;
/*
* 實(shí)現(xiàn)接口
* 在實(shí)現(xiàn)時的第一種情況:已經(jīng)知道類型
* 第二種:不知道類型
*/
public class InterImpl implements Inter<String>{
@Override
public void show(String t) {
// TODO Auto-generated method stub
System.out.println(t);
}
}
package cn.itcast_03;
/*
* 測試
*/
public class InterDemo {
public static void main(String[] args) {
//第一種的測試
Inter<String> i=new InterImpl();
i.show("hello");
}
}
這種的并不常用,下面是第二種情況:
public class InterImpl<T> implements Inter<T>{
@Override
public void show(T t) {
// TODO Auto-generated method stub
System.out.println(t);
}
}
//第二種測試
Inter<String> i=new InterImpl<String>();
i.show("hello");
Inter<Integer> ii=new InterImpl<Integer>();
ii.show(100);
Inter<Boolean> iii=new InterImpl<Boolean>();
iii.show(true);
-
泛型高級
1苟跪、泛型通配符<?>
2廷痘、?extends E:向下限定蔓涧,E及其子類
?是Animal的子類或者本身笋额,而第一個Object是Animal的父類元暴;
3、兄猩?super E:向上限定茉盏,E及其父類
與上面的恰好相反; - 增強(qiáng)for
package cn.itcast_04;
import java.util.ArrayList;
import java.util.Iterator;
/*
* 增強(qiáng)for:是for循環(huán)的一種
* 好處:簡化了數(shù)組和集合的遍歷枢冤;
*/
public class ForDemo {
public static void main(String[] args) {
//定義一個int數(shù)組
int[] arr={1,2,3,4,5};
//普通的遍歷
for (int x = 0; x < arr.length; x++) {
System.out.println(arr[x]);
}
System.out.println("------");
//增強(qiáng)for
for(int x:arr){
System.out.println(x);
}
System.out.println("-------");
//定義一個字符串?dāng)?shù)組
String[] strArray={"李六","王五","李四","張三"};
//增強(qiáng)for
for(String x:strArray){
System.out.println(x);
}
System.out.println("-------");
//定義一個集合
ArrayList<String> arrayList=new ArrayList<String>();
arrayList.add("hello");
arrayList.add("world");
arrayList.add("java");
//增強(qiáng)for
for(String x:arrayList){
System.out.println(x);
}
}
}
下面是她的弊端:
List<String> list=null;
for(String s:list){
System.out.println(s);
}
說明增強(qiáng)for循環(huán)不能為空鸠姨,但是怎么解決呢?就是先判斷淹真,再執(zhí)行:
List<String> list=null;
if (list!=null) {
for(String s:list){
System.out.println(s);
}
}
因?yàn)榍懊娴臄?shù)組是空的讶迁,所以執(zhí)行沒有結(jié)果;
-
練習(xí):
要求: