Java 泛型

泛型

表示集合中 保存的数据的类型

泛型类

泛型类声明是 尖括号的字母 可以随便定义
泛型类的类型 在初始化这个类的对象 确定
public class Worker<W> {
    // 利用泛型写成员变量
    private W w;

    // 利用泛型写set/get方法
    public W getW() {
        return this.w;
    }

    public void setW(W w) {
        this.w = w;
    }

    // 普通成员方法
    public void sayHi(W w) {
        System.out.println(w);
    }

    // 除了W 还能不能使用其他的泛型 例如 Z
    // 声明不同泛型的方法 在调用泛型方法的时候 指定泛型的类型
    // <Z> 声明一个泛型 只有声明过 才能使用
    public <Z> void print(Z z) {
        System.out.println(z);
    }

    // 静态方法中能不能 直接使用 w
    // 当你调用静态方法的时候 可能还没有对象
    // 没有对象就没指定泛型 所以不能用
    public static <Q> void fun(Q q) {
        System.out.println(q);

    }

    // 工作
    public void work() {
        System.out.println(" 天天搬砖  一天搬400块 ");
    }
}

泛型接口

interface InterA<Y>{
    void fun(Y y);
}
实现类
class InterAImpl implements InterA<String>{
    @Override
    public void fun(String y) {
        System.out.println(y);
    }
}

    /**
     * 保存字符串
     */
    public static void fun1() {
        // 创建一个集合 保存 a b c d
        // <E> E就代表 要保存的元素类型
        // 后面的尖括号 要跟前面填的泛型 保持一致
        // JDK 1.7 菱形泛型 
        // 如果前面声明了泛型 后面泛型可以省略不写 省略不写 表示类型一致
        ArrayList<String> list = new ArrayList<String>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        // 用迭代器遍历
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            // 添加泛型之后 可以省去 强转类型的麻烦
            String next = iterator.next();
            System.out.println(next);
        }
    }
    /**
     * 写泛型编译期就会报错
     */
    public static void fun2() {
        // 创建集合 保存三学生
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("张三",15));
        students.add(new Student("李四",16));
        students.add(new Student("王五",17));
        // 获取迭代器
        Iterator iterator = students.iterator();
        Student student = (Student)iterator.next();
        System.out.println(student.getName());
        // 编译通过 是这个类型 你才能强转 运行报错
        // 设置泛型 可以把运行期报错 转化到 编译期就报错
        // Worker worker = (Worker)iterator.next();
        // worker.work();
    }
    public static void fun3() {
        // 创建泛型类
        Worker<String> worker = new Worker<>();
        // 给属性赋值
        worker.setW("haha");
        System.out.println(worker.getW());
        // 调用成员方法
        worker.sayHi("hahaha");
        // 调用方法给定类型
        worker.print("???");
        // 调用静态方法
        Worker.fun("w");
    }

? extends E (向下限定)

? 是子类 继承 E是父类 ?号只能是E类的 子类

? super E (向上限定)

? 父类 E是子类 ?只能是E类的父类
    public static void fun1() {
        /*
         * 创建一个保存人的集合 存俩人
         * 创建一个保存学生的集合 存俩人
         */
        ArrayList<Person> pList = new ArrayList<>();
        pList.add(new Person("张三", 13));
        pList.add(new Person("李四", 14));

        ArrayList<Student> sList = new ArrayList<>();
        sList.add(new Student("王五", 16));
        sList.add(new Student("赵六", 17));
        // 学生的集合 全部添加到人的集合中
        // ? extends Person 只能填Person的子类
        pList.addAll(sList);
        // sList.addAll(pList);
        System.out.println(pList);
    }
        // Arrays 类方法 把数组转化为集合 
        int[] array = {1,2,3,4,5}; // 没有进行自动装箱
        // 把数组 当做集合中的一个元素 转为了集合
        List<int[]> asList = Arrays.asList(array);
        System.out.println(asList);

        Integer[] array2 = {1,2,3,4,5}; // 自动装箱为 Integer类型
        List<Integer> asList2 = Arrays.asList(array2);
        System.out.println(asList2);

        String[] array3 = {"wanglong","wangsong"};
        // 数组转集合
        List<String> asList3 = Arrays.asList(array3);
        System.out.println(asList3);
        // 使用asList数组转集合 得到一个集合
        // 注意: 这个集合 不允许 进行添加或删除的操作
        // 这么转 意义何在?
        boolean isContions = asList3.contains("wanglong");
        // asList3.add("wangjianzhong");
        System.out.println(asList3);
        System.out.println(isContions);
    // int ... num 相当于 传入的参数 是个数组
    // int ... num 可以接受多个参数 只能是方法参数最后一个
    // JDK 1.5 之后出来的
    public static void fun2(int ... num) {
        // 遍历
        for (int i = 0; i < num.length; i++) {
            System.out.println(num[i]);
        }
    }

集合中的删除方式

1. 不使用迭代器
    public static void fun1() {
        // 创建集合 保存 a b c d e
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");
        // 如果不使用迭代器遍历
        // 如果集合中有b 就把b删除
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).equals("b")) {
                // 先当参数传进去 再进行自减
                list.remove(i--);
            }
        }
        System.out.println(list);
    }
2. 使用迭代器
    public static void fun2() {
        // 迭代器删除
        // 创建集合 保存 a b c d e
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("b");
        list.add("c");
        list.add("d");
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            if (next.equals("b")) {
                iterator.remove();
            }
        }
        System.out.println(list);
    }

// 创建一个结合 存入5个学生 按学生年龄 进行排序

ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("c", 19));
        list.add(new Student("e", 18));
        list.add(new Student("a", 13));
        list.add(new Student("d", 16));
        list.add(new Student("b", 17));
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                // 比较两个学生 j 和 j + 1 年龄
                Student s1 = list.get(j);
                Student s2 = list.get(j + 1);
                // 比较年龄
                if (s1.getAge() > s2.getAge()) {
                    // 交换在集合中的位置
                    // set(int index, Obj obj)
                    list.set(j + 1 , s1);
                    list.set(j , s2);

                }
            }
        }
        System.out.println(list);
// 创建一个结合 存入5个学生 按姓名 进行排序 封装成方法
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("c", 19));
        list.add(new Student("e", 18));
        list.add(new Student("a", 13));
        list.add(new Student("d", 16));
        list.add(new Student("b", 17));
        // 调用方法
        paiXu(list);
        System.out.println(list);

    }
    // 按姓名比较 传进来的是集合的地址
    public static void paiXu(ArrayList<Student> array) {
        for (int i = 0; i < array.size() - 1; i++) {
            for (int j = 0; j < array.size() - 1 - i; j++) {
                Student s1 = array.get(j);
                Student s2 = array.get(j + 1);
                if (s1.getName().compareTo(s2.getName()) > 0) {
//                  array.set(j + 1, s1);
//                  array.set(j, s2);
                    Collections.swap(array, j , j + 1);
                }
            }
        }
    }

        /*
         * 创建一个集合保存 java学科
         * 学科中有2个班(集合 保存的是学生) 班里有学生(使用泛型)
         * 遍历java学科中的学生
         */
        // 声明学科的集合
        ArrayList<ArrayList<Student>> subject = new ArrayList<>();
        // 声明班
        ArrayList<Student> classes1 = new ArrayList<>();
        classes1.add(new Student("张三", 14));
        classes1.add(new Student("李四", 11));
        ArrayList<Student> classes2 = new ArrayList<>();
        classes2.add(new Student("赵六", 12));
        classes2.add(new Student("王五", 13));

        // 把班添加到 学科中 
        subject.add(classes1);
        subject.add(classes2);
        // 遍历学科中的学生
        // 通过学科 找到班
        for (ArrayList<Student> list : subject) {
            // 通过班找到学生
            for (Student student : list) {
                System.out.println(student);
            }
        }
        // 迭代器遍历
        Iterator<ArrayList<Student>> arrayIterator = subject.iterator();
        while (arrayIterator.hasNext()) {
            ArrayList<Student> arrayList = arrayIterator.next();
            Iterator<Student> iterator = arrayList.iterator();
            while (iterator.hasNext()) {
                Student next = iterator.next();
                System.out.println(next);
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值