我是靠谱客的博主 包容河马,这篇文章主要介绍【JavaSE_学习笔记】泛型,现在分享给大家,希望可以做个参考。

【JavaSE_学习笔记】泛型

泛型格式:<引用类型>:泛型只能放引用类型
泛型的好处:
  1.可以把运行时出现的问题提前至编译时
  2.避免了无谓的强制类型转换
  3.解决了黄色警告线的问题
问题:以下哪些写法是正确的?

复制代码
1
2
3
4
5
6
7
8
ArrayList<String> list = new ArrayList<String>(); true ArrayList<Object> list = new ArrayList<String>(); false ArrayList<String> list = new ArrayList<Object>(); false // 为了新老系统的兼用性 ArrayList list = new ArrayList<String>(); true ArrayList<String> list = new ArrayList(); true

注意:
  1.泛型中没有多态的概念,左右两边的数据类型必须一致,或是直写一边的泛型
  推荐:两边都写同样数据类型的泛型
  2.泛型中是不能使用基本数据类型数据的,如需使用则要使用基本数据类型对应的包装类型(如:int——->Integer)

自定义泛型:

自定义泛型:可以理解为一个数据类型的变量或是一个数据类型的占位符

方法自定义泛型:

方法自定义泛型的格式:

复制代码
1
2
3
修饰符 <声明自定义泛型> 返回值类型 方法名 (形参类表){ }

举例:

复制代码
1
2
3
4
//返回值类型与形参一致 public static <T> T print(T t){ return t; }

方法自定义泛型注意:
  1.方法上自定义泛型的具体数据类型是在调用该方法时,传递实参数据时确定具体数据类型的
  2.自定义泛型使用的标识符可以自定义,只要符合标识符的命名规则即可,但一般自定义泛型的标识符都是单个大写字母
练习:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
public class Demo2 { public static void main(String[] args) { String str = print("abc"); Integer num = print(123); } public static <T> T print(T o){ // T E return o; } }

泛型类:

自定义泛型类的格式:

复制代码
1
2
3
class 类名 <自定义泛型>{ }

泛型类注意:
  1.类上声明的自定义泛型的具体数据类型是在使用该类创建对象时确定的;
  2.如果一个类已经声明了自定义泛型类,该类在创建对象时没有指定自定义泛型的具体数据类型,默认为Object类型;
  3.静态方法不能使用类上声明的自定义泛型,如需使用自定义泛型只能在自己的方法上声明
练习:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.util.ArrayList; class MyArrays<T>{ //翻转 public void reverse(T[] arr){ for(int startIndex = 0 , endIndex= arr.length-1; startIndex<endIndex ; startIndex++,endIndex--){ T temp = arr[startIndex]; arr[startIndex] = arr[endIndex]; arr[endIndex] = temp; } } public String toString(T[] arr){ StringBuilder sb = new StringBuilder(); for(int i = 0 ; i<arr.length ; i++){ if(i==0){ sb.append("["+arr[i]+","); }else if(i==arr.length-1){ sb.append(arr[i]+"]"); }else{ sb.append(arr[i]+","); } } return sb.toString(); } public static <T> void test(T[] arr){ } } public class Demo3 { public static void main(String[] args) { Integer[] arr = {1,2,3,4,5}; MyArrays tool = new MyArrays(); tool.reverse(arr); System.out.println("数组的元素:"+ tool.toString(arr)); } }

泛型接口:

泛型接口的定义格式:

复制代码
1
2
3
interface 接口名 <声明自定义的泛型>{ }

举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
interface Dao<T>{ public void add(T t); } class UserDao implements Dao<String>{ @Override public void add(String t) { }

泛型接口注意:
  1.接口上自定义泛型的具体数据类型是在实现该接口时确定的;
  2.如果一个接口已经自定义了泛型,在实现该接口时没有指定自定义泛型的具体数据类型,则默认为Object类型
举例:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
interface Dao<T>{ public void add(T t); } //在创建接口实现类对象时再指定接口的自定义泛型的具体数据类型 class UserDao<T> implements Dao<T>{ @Override public void add(T t) { } } public class Demo4 { public static void main(String[] args) { UserDao<String> dao = new UserDao<String>(); dao.add("abc"); } }

泛型的上下限:

泛型的通配符:?  可以匹配任意类型
一般不单独使用

复制代码
1
2
3
4
//需求1: 定义一个方法可以接受任意类型的集合对象, 接收的集合对象只能存储Integer以及Integer父类类型的数据。 public static void print(Collection<? super Integer> c){ }
复制代码
1
2
3
4
5
// 需求2: 定义一个方法可以接受任意类型的集合对象, 接收的集合对象只能存储Number或者Number子类类型的数据。 public static void show(Collection<? extends Number> c){ }

? super Integer 泛型的下限 只能用于Integer或者是Integer的父类类型数据
? extends Number 泛型的上限 只能用于Number或者是Number的子类类型数据

集合的嵌套遍历:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.ArrayList; /** * 假设有一个Java班,看成一个容器,这个容器里面有很多学生 * ArrayList<Student>,但是又不止一个Java班, * 大的集合:ArrayList<ArrayList<Student>> * 集合的嵌套遍历 * @author Apple */ public class ArrayListTest { public static void main(String[] args) { // 创建一个大集合的对象 ArrayList<ArrayList<Student>> bigArray = new ArrayList<ArrayList<Student>>(); //创建第一个子集合对象ArrayList<Student> ArrayList<Student> firstArray = new ArrayList<Student>() ; //创建学生对象 Student s1 = new Student("唐僧", 30) ; Student s2 = new Student("猪八戒", 22) ; Student s3 = new Student("孙悟空", 26) ; //给第一个集合中添加元素 firstArray.add(s1) ; firstArray.add(s2) ; firstArray.add(s3) ; //将第一个子集合添加到大集合中 bigArray.add(firstArray) ; //创建第二个子集合对象 ArrayList<Student> secondArray = new ArrayList<Student>() ; //创建学生对象 Student s4 = new Student("高圆圆", 30) ; Student s5 = new Student("邓超", 22) ; Student s6 = new Student("朱亚文", 26) ; //给第二个子集合中添加元素 secondArray.add(s4) ; secondArray.add(s5) ; secondArray.add(s6) ; //将第二个子集合添加到大集合中 bigArray.add(secondArray) ; //创建第二个子集合对象 ArrayList<Student> thirdArray = new ArrayList<Student>() ; //创建学生对象 Student s7 = new Student("高圆圆", 30) ; Student s8 = new Student("邓超", 22) ; Student s9 = new Student("朱亚文", 26) ; //给第二个子集合中添加元素 thirdArray.add(s7) ; thirdArray.add(s8) ; thirdArray.add(s9) ; //将第二个子集合添加到大集合中 bigArray.add(thirdArray) ; //遍历大集合 //增强for遍历 //ArrrayList<ArrayList<student>> for(ArrayList<Student> array :bigArray){ //子集合:ArrayList<Student> for(Student s : array){ System.out.println(s.getName()+"---"+s.getAge()); } } } }

最后

以上就是包容河马最近收集整理的关于【JavaSE_学习笔记】泛型的全部内容,更多相关【JavaSE_学习笔记】泛型内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(111)

评论列表共有 0 条评论

立即
投稿
返回
顶部