泛型概念
泛型:是一种未知的数据类型,当我们不知道使用什么数据类型时,可以使用泛型。
泛型也可以看作是一个变量,用来接收数据类型
- E e :元素
- T t :类型
使用泛型的好处与弊端
创建集合对象,使用泛型
好处:
- 1.避免了类型转化的麻烦,存储的是什么类型,取出的就是什么类型
- 2.把运行期异常(代码运行后会抛出的异常),提升到编译期(写代码时报错)
- 弊端:
- 泛型是什么类型,就只能存储什么类型的数据
创建集合对象,不使用泛型 - 好处:
- 集合不使用泛型,默认的类型就是Object类型,可以存储任意类型的数据
- 弊端:
- 不安全,会引发异常。
public class Demo01 {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<>();//创建带有泛型的集合
arr.add("abc");
// arr.add(1);报错
Iterator<String> it = arr.iterator();//迭代器
while (it.hasNext()){//遍历
String next = it.next();
System.out.println(next + next.length());
}
//创建不带泛型的集合
ArrayList list = new ArrayList();
list.add("cba");
list.add(1);
Iterator itt = list.iterator();
while (itt.hasNext()){
Object next = itt.next();
// System.out.println(next);ClassCastException异常
//因为是ArrayList,想要获得字符串长度就需要向下转型
String str = (String)next;
System.out.println(str.length());
}
}
}
定义含有泛型的类
定义一个含有泛型的类,模拟ArrayList集合
泛型是一个未知的数据类型,我们不确定用什么数据类型时,可以使用泛型
泛型可以接收任意的数据类型,可以使用Integer,String,Student
创建对象的时候确定泛型的数据类型
public class GenericClass<E> {
private E name;
public E getName() {
return name;
}
public void setName(E name) {
this.name = name;
}
}
public class Demo01GenericClass {
public static void main(String[] args) {
GenericClass gc = new GenericClass();
gc.setName("abc");
Object name = gc.getName();
System.out.println(name);
//创建GenericClass对象,泛型使用Integer类型。
GenericClass<Integer> gc2 = new GenericClass<>();
gc2.setName(1);
Integer name1 = gc2.getName();
System.out.println(name1);
//创建GenericClass对象,泛型使用String类型。
GenericClass<String> gc3 = new GenericClass<>();
gc3.setName("sx");
String name2 = gc3.getName();
System.out.println(name2);
}
}
定义含有泛型的方法
定义含有泛型的方法:泛型定义在方法的修饰符和返回值类型之间
- 格式:
-
修饰符 <泛型> 返回值类型 方法名(参数列表(使用泛型)){
-
方法体;
*}
含有泛型的方法,在调用方法的使用确定泛型的数据类型
传递什么类型的参数,泛型就是什么类型
public class GenericMethod {
//定义一个含有泛型的方法
public <E> void method(E e){
System.out.println(e);
}
public static <M> void method2(M m){
System.out.println(m);
}
}
public class Demo02GenericMethod {
public static void main(String[] args) {
//创建对象
GenericMethod gc = new GenericMethod();
//调用含有泛型的方法method1,传递什么类型,泛型就是什么类型
gc.method("100");
gc.method("哈哈");
gc.method(1);
gc.method(6.6);
//静态方法调用,两种方法,对象点,类点
gc.method2(12);
GenericMethod.method2("ahha");
}
}
在接口中定义泛型
含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的泛型,创建对象调用方法
- public interface Iterator{
-
E next();
- }
Scanner类实现了Iterator接口,并指定接口的泛型为String,所以重写的next方法泛型默认就是String - public final class Scanner implements Iterator{
-
public String next(){}
- }
含有泛型的接口,第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走 - 就相当于定义了一个含有泛型的类,创建对象时确定泛型的类型
- public interface List{
- boolean add(E e);
- E get(int index);
- }
- public class ArrayList implements List{
- public boolean add(E e){}
- public E get(int index){}
- }
public interface GenericInterface<I> {
public abstract void method(I i);
}
第一种方法
public class GenericInterfaceImpl implements GenericInterface<String>{
@Override
public void method(String s) {
System.out.println(s);
}
}
第二种方法
public class GenericInterfaceImpl2<E> implements GenericInterface<E>{
@Override
public void method(E e) {
}
}
public class Demo03GenericInterface {
public static void main(String[] args) {
//创建GenericInterfaceImpl对象
GenericInterfaceImpl gc = new GenericInterfaceImpl();
gc.method("llllll");
//创建GenericInterfaceImpl2对象
GenericInterfaceImpl2<Integer> gc2 = new GenericInterfaceImpl2<>();
gc2.method(10);
GenericInterfaceImpl2<Double> gc3 = new GenericInterfaceImpl2<>();
gc3.method(2.2);
}
}
泛型的通配符
泛型的通配符: ?:代表任意的数据类型
- 使用方式:不能创建对象使用,只能作为方法的参数使用
- 如果要定义一个方法,能遍历所有类型的ArrayList集合
- 这时我们不知道ArrayList集合使用什么数据类型,可以使用泛型的通配符?来接收数据类型
- 注意:泛型没有继承概念
public class Demo05Generic {
public static void main(String[] args) {
ArrayList<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
ArrayList<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
printArray(list1);
printArray(list2);
}
public static void printArray(ArrayList<?> list){
//使用迭代器遍历集合
Iterator<?> it = list.iterator();
while (it.hasNext()){
Object next = it.next();
System.out.println(next);
}
}
}