Java的泛型

定义:
集合中保存数据的类型

创建泛型:
//  创建一个集合 保存 a b c d
public static void fun() {
    //  <E> E代表 要保存的元素类型
    //  后面的尖括号 要和前面填的泛型 保持一致
    //  JDK1.7出来 菱形泛型
    //  如果前面声明了泛型 后面泛型可以省略不写 省略不写 表示类型一致
    ArrayList<String> list = new ArrayList<>();
    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);
    }
}

人类:
/*
 * 姓名 和 年龄
 * 构造 set/get toString
 * 创建一个学生类(构造方法 toString)继承 人类
 */
public class Person {
    private String name;
    private int age;
    public Person() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Person(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    @Override
    public String toString() {
        return "姓名:" + name + " 年龄:" + age;
    }
}

学生类:
public class Student extends Person {
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Student(String name, int age) {
        super(name, age);
        // TODO Auto-generated constructor stub
    }
}

泛型编译期报错:
public static void fun() {
    //  创建集合 保存三学生
    ArrayList<Student> students = new ArrayList<>();
    students.add(new Student("鹏鹏", 17));
    students.add(new Student("水水", 17));
    students.add(new Student("茜茜", 17));
    //  获取迭代器
    Iterator<Student> iterator = students.iterator();
    Student student = iterator.next();
    System.out.println(student);

    //  是一个类型 才能强转
    //  设置泛型 可以把运行期报错 转化到 编译器报错
    //Worker work = (Worker)iterator.next();
}

泛型类:
/*
 * 泛型类(泛型类声明时 尖括号中的字母 可以随便定义)
 * 泛型类的类型 在初始化这个类的对象时 确定
 * 
 * 要求能看懂就行
 */
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<S> void fun(S s) {
        System.out.println(s);
    }

    //  工作
    public void work() {
        System.out.println("天天搬砖 一天400块");
    }
}
public static void fun() {
    //  创建泛型类
    Worker<String> worker = new Worker<>();
    //  给属性赋值
    worker.setW("哈哈");      
    System.out.println(worker.getW());
    //  调用成员方法
    worker.sayHi("哈哈哈");
    //  调用方法给定类型
    worker.print("哈哈哈哈");
    //  调用静态方法
    Worker.fun("哈哈哈哈哈");
}

泛型接口:
interface InterA<A> {
    public abstract void fun(A a);
}

//  实现类
class InterAImpl implements InterA<String>{
    @Override
    public void fun(String a) {
        // TODO Auto-generated method stub
        System.out.println(a);
    }
}

? extends E(向下限定):
? 是子类  继承    E 是父类  问号只能是E类的子类

public static void fun1() {
    /*
     * 创建一个保存人的集合 存两人
     * 创建一个保存学生的集合 存两人
     */
    ArrayList<Person> pList = new ArrayList<>();
    pList.add(new Person("张三", 11));
    pList.add(new Person("李四", 11));

    ArrayList<Student> sList = new ArrayList<>();
    sList.add(new Student("王五", 14));
    sList.add(new Student("赵六", 15));
    //  学生的集合 全部添加到人的集合中
    //  ? extends Person 只能填Person的子类
    pList.addAll(sList);
    System.out.println(pList);
}

? super E(向上限定):
? 是父类  E是子类 问号只能是E类的父类

//  int ... num 相当于 传入的参数是个数组
//  int ... num 可以接受多个参数 只能是方法参数的最后一个
//  JDK1.5出来的
public static void fun(int j, int ... num) {
    //  遍历
    for (int i = 0; i < num.length; i++) {
        System.out.println(num[i]);
    }
    System.out.println("J是:" + j);
}

举例:
public static void fun() {
    //  Arrays 类方法 把数组转化为集合
    int[] array = new int[]{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.err.println(asList2);

    String[] array3 = {"wonglong", "wangsong"};
    //  数组转集合
    List<String> asList3 = Arrays.asList(array3);
    System.out.println(array3);
    //  把王建忠 添加集合中
    //  使用asList数组转集合 得到一个集合
    //  注意:这个集合不允许 进行添加或删除的操作
    //  这么转 意义何在? 可以调用集合的其他方法
    boolean contains = asList3.contains("wanglong");
    //asList3.add("wangjianzhong");
    System.out.println(asList3);
    System.out.println(contains);
}

集合中删除:

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

使用迭代器:
public static void fun() {
    //  迭代器删除
    ArrayList<String> list = new ArrayList<>();
    list.add("a");
    list.add("b");      
    list.add("c");
    list.add("b");
    list.add("d");
    list.add("e");
    //  获取迭代器
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
        String string = iterator.next();
        if (string.equals("b")) {
            //  使用迭代器删除
            iterator.remove();
        }
    }
    System.out.println(list);
}

foreach:
public static void fun() {
    //  创建集合 保存 a b c d e
    ArrayList<String> list = new ArrayList<>();
    list.add("a");
    list.add("b");      
    list.add("c");
    list.add("b");
    list.add("d");
    list.add("e");
    //  增强for循环遍历 底层使用迭代器遍历
    //  增强for循环 只能 用来遍历 也叫快速便利
    //  双层遍历的时 使用的比较多
    for (String string : list) {
        System.out.println(string);
    }
}
实例应用:
public static void fun() {
    //  创建一个集合 存入5个学生 按学生年龄 进行排序
    ArrayList<Student> list = new ArrayList<>();
    list.add(new Student("张三", 19));
    list.add(new Student("李四", 25));
    list.add(new Student("王五", 17));
    list.add(new Student("赵六", 23));
    list.add(new Student("孙七", 18));
    for (int i = 0; i < list.size() - 1; i++) {
        for (int j = 0; j < list.size() - 1 - i; j++) {
            //  比较两个学生 年龄
            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);
}

public static void fun() {
    //  创建一个集合 存入5个学生 按姓名 进行排序 封装成方法
    ArrayList<Student> list = new ArrayList<>();
    list.add(new Student("张三", 19));
    list.add(new Student("李四", 25));
    list.add(new Student("王五", 17));
    list.add(new Student("赵六", 23));
    list.add(new Student("孙七", 18));
    for (int i = 0; i < list.size() - 1; i++) {
        for (int j = 0; j < list.size() - 1 - i; j++) {
            //  获取学生
            Student s1 = list.get(j);
            Student s2 = list.get(j + 1);
            //  比较姓名
            if (s1.getName().compareTo(s2.getName()) > 0) {
                //  交换
                Collections.swap(list, j, j + 1);
            }
        }
    }
    System.out.println(list);
}

public static void fun() {
    /*
     * 创建一个集合保存 java学科(集合)
     * 学科中有两个班(集合) 班里有学生(使用泛型)
     * 遍历java学科中的学生
     */
    //  声明学科的集合
    ArrayList<ArrayList<Student>> subject = new ArrayList<>();

    //  声明班
    ArrayList<Student> class1 = new ArrayList<>();
    class1.add(new Student("张三", 15));
    class1.add(new Student("李四", 16));

    ArrayList<Student> class2 = new ArrayList<>();
    class2.add(new Student("王五", 18));
    class2.add(new Student("赵六", 16));

    //  把班添加到学科中
    subject.add(class1);
    subject.add(class2);

    /*
    //  遍历学科中的学生
    //  通过学科 找到班
    for (ArrayList<Student> list : subject) {
        //  通过班找到学生
        for (Student student : list) {
            //  打印学生
            System.out.println(student);
        }
    }
    */

    Iterator<ArrayList<Student>> sub = subject.iterator();
    while (sub.hasNext()) {
        ArrayList<Student> aList = sub.next();
        Iterator<Student> classes = aList.iterator();
        while (classes.hasNext()) {
            Student student = classes.next();
            System.out.println(student);
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值