• 【数据结构】初识泛型


    文章目录


    一般的类和方法,只能使用具体的类型: 要么是基本类型,要么是自定义的类。这种限制对代码的束缚就会很大。所以我们引入了泛型。泛型,泛顾名思义就是广泛的意思。就是适用于许多许多类型。从代码上讲,就是对类型实现了参数化。

    我们来看下面的代码:

    public class Demo03 {
        public static void main(String[] args) {
            MyArray myArray = new MyArray();
            myArray.setValue(0,10);
            myArray.setValue(1,"hello");
            String pos = myArray.getPos(0); //error
            System.out.println(pos);
        }
    }
    
    class MyArray {
        Object[] arr = new Object[10];
    
    	//获取下标为pos的元素
        public Object getPos(int pos) {
            return this.arr[pos];
        }
    	//在pos位置放入val元素
        public void setValue(int pos,Object val) {
            this.arr[pos] = val;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    通过上面的代码我们发现,虽然在这种情况下,当前数组任何数据都可以存放,但是,更多情况下,我们还是希望他只能够持有一种数据类型。而不是同时持有这么多类型。

    泛型语法:

    class 泛型类名称<类型形参列表> {
    // 这里可以使用类型参数
    }
    class ClassName<T1, T2, ..., Tn> {
    }
    class 泛型类名称<类型形参列表> extends 继承类/* 这里可以使用类型参数 */ {
    // 这里可以使用类型参数
    }
    class ClassName<T1, T2, ..., Tn> extends ParentClass<T1> {
    // 可以只使用部分类型参数
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    我们可以把上面的代码用泛型进行改写:

    public class Demo03 {
        public static void main(String[] args) {
            MyArray<Integer> myArray = new MyArray<>();
            myArray.setValue(0,10);
            myArray.setValue(1,11);
        }
    }
    
    class MyArray<T> {
        T[] arr = (T[])new Object[10];
    
        public T getPos(int pos) {
            return this.arr[pos];
        }
        public void setValue(int pos,T val) {
            this.arr[pos] = val;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    代码解释:
    类名后的 尖括号T 代表占位符,表示当前类是一个泛型类
    不能new泛型类型的数组,比如T[] t = new T[5];
    类型后加入 Integer 指定当前类型
    不需要进行强制类型转换
    编译器会在存放元素的时候帮助我们进行类型检查。

    泛型类的使用

    泛型类<类型实参> 变量名; // 定义一个泛型类引用
    new 泛型类<类型实参>(构造方法实参); // 实例化一个泛型类对象
    
    • 1
    • 2

    比如:

    MyArray<Integer> list = new MyArray<Integer>();
    //等价于 MyArray list = new MyArray<>();
    
    • 1
    • 2

    注意:泛型只能接受引用类型,所有的基本数据类型必须使用包装类。
    泛型的上界
    在定义泛型类时,有时需要对传入的类型变量做一定的约束,可以通过类型边界来约束。

    class 泛型类名称<类型形参 extends 类型边界> {
    //
    }
    
    • 1
    • 2
    • 3

    比如:

    public class MyArray<E extends Number> {
    //
    }
    
    • 1
    • 2
    • 3

    这样它就只接受 Number 的子类型作为 E 的类型实参。

    MyArray<Integer> str1; // 正常,因为 Integer 是 Number 的子类型
    MyArray<String> str2; // 编译错误,因为 String 不是 Number 的子类型
    
    • 1
    • 2

    我们来看这道练习:写一个泛型,实现一个方法,方法是求指定类型数组的最大值

    public class Demo02 {
        public static void main(String[] args) {
            FindMaxValue<Integer> tFindMaxValue = new FindMaxValue<>();
            Integer[] array = {1,4,6,2,10};
            Integer max = tFindMaxValue.findMax(array);
            System.out.println(max);
        }
    }
    
    //泛型类
    //>  T一定是实现了Comparable接口
    class FindMaxValue <T extends Comparable<T>> {
        public T findMax(T[] arr) {
            T max = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (max.compareTo(arr[i])<0){
                    max = arr[i];
                }
            }
            return max;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    上面的代码我们也可以使用泛型方法来写

    public class Demo02 {
        public static void main(String[] args) {
            Integer[] array1 = {1,4,6,2,10};
            Integer max1 = FindMaxValue1.findMax(array1);
            System.out.println(max1);
        }
    }
    
    class FindMaxValue1 {
        //泛型方法
        public static <T extends Comparable<T>> T findMax(T[] arr) {
            T max = arr[0];
            for (int i = 1; i < arr.length; i++) {
                if (max.compareTo(arr[i])<0){
                    max = arr[i];
                }
            }
            return max;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
  • 相关阅读:
    Pycharm开发环境下创建python运行的虚拟环境(自动执行安装依赖包)
    ubuntu 22.04 flameshot 截图异常的问题
    非零基础自学Java (老师:韩顺平) 第8章 面向对象编程(中级部分) 8.13 断点调试(debug)
    盐城北大青鸟军训剪影丨磨炼钢铁意志,绽放迷彩青春
    iOS 集成Jenkins 完整流程 (自由风格)
    复旦微FMQL20SM全国产ARM+FPGA核心板,替代xilinx ZYNQ7020系列
    基于ssm的视力保养连锁预约系统设计与实现-计算机毕业设计源码+LW文档
    卡那霉素(Kanamycin偶联卵清白蛋白 (KAN-OVA)
    记录下双系统
    java毕业设计现有传染病查询系统mybatis+源码+调试部署+系统+数据库+lw
  • 原文地址:https://blog.csdn.net/qq_58032742/article/details/132829825