zoukankan      html  css  js  c++  java
  • java泛型

    一.泛型的引入

    集合中是可以存放任意对象的,导致取出时,如果出现强转就会引发运行时 ClassCastException。

    使用集合时,必须明确集合中元素的类型。这种方式称为:泛型。

    public class GenericDemo {
        public static void main(String[] args) {
            List list new ArrayList();
            list.add("abc");
            list.add("oracle");
            list.add(5);//由于集合没有做任何限定,任何类型都可以给其中存放
            Iterator it = list.iterator();
            while(it.hasNext()){
                //需要打印每个字符串的长度,就要把迭代出来的对象转成String类型
                String str = (String) it.next();
                System.out.println(str.length());
            }
        }
    }

    程序在运行时发生了问题java.lang.ClassCastException

    二.泛型的使用

    1.含有泛型的类

    class ArrayList<E>{
    public boolean add(E e){ }
        public E get(int index){ }
    }
    //使用格式:创建对象时,确定泛型的类型
    ArrayList<String> list = new ArrayList<String>();

    变量E的值就是String类型

    class ArrayList<String>{
    public boolean add(String e){ }
        public String get(int index){ }
    }
    ArrayList<Integer> list = new ArrayList<Integer>();

    变量E的值就是Integer类型

    class ArrayList<Integer>{
    public boolean add(Integer e){ }
        public Integer get(int index){ }
    }

    2.含有泛型的接口

    定义格式:修饰符 interface接口名<代表泛型的变量> { }

    例如,API中的Iterator迭代器接口

    public interface Iterator<E> {
        public abstract E next();
    }

    使用格式:

    1.定义类时确定泛型的类型

    public final class Scanner implements Iterator<String> {
      public String next(){ }
    }

    变量E的值就是String类型

    2.始终不确定泛型的类型,直到创建对象时,确定泛型的类型

    ArrayList<String> list = new ArrayList<String>();
    Iterator<String> it = list.iterator();

    变量E的值就是String类型

    public interface Iterator<String> {
        public abstract String next();
    }

    三.泛型的好处

    将运行时期的ClassCastException,转移到了编译时期变成了编译失败。避免了类型强转的麻烦。

    public class GenericDemo {
        public static void main(String[] args) {
            List<String> list = new ArrayList<String>();
            list.add("abc");
            list.add("oracle");
            //list.add(5);//当集合明确类型后,存放类型不一致就会编译报错
            //集合已经明确具体存放的元素类型,那么在使用迭代器的时候,迭代器也同样会知道具体遍历元素类型
            Iterator<String> it = list.iterator();
            while(it.hasNext()){
        String str = it.next();
    //当使用Iterator<String>控制元素类型后,就不需要强转了。获取到的元素直接就是String类型
                System.out.println(str.length());
            }
        }
    }

    四.泛型通配符

    java提供了泛型的通配符<?>

    public static void printCollection(Collection<?> list) {
        Iterator<?> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }

    当使用泛型类或者接口时,传递的数据中,泛型类型不确定,可以通过通配符<?>表示。但是一旦使用泛型的通配符后,只能使用Object类中的共性方法,集合中元素自身方法无法使用。

    五.泛型限定

    1.限定泛型的上限:

    格式:? extends E

    ? 代表接收E类型或者E的子类型的元素

    例如:泛型限定为:? extends Person

    2.限定泛型的下限:

    格式:? super E

    ? 代表接收E类型或者E的父类型的元素

    例如,泛型限定为:? super Student

    则 ? 代表接收Student类型或者Student父类型的元素

    例子:

    //修改下面的方法,使该方法可以打印学生和工人的集合
    
    class Student extends Person{ }
    class Worker extends Person{ }
    public static void printCollection(Collection<?> list) {
        Iterator<?> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
     }}

    ? extends Person : 接收Person类型或者Person的子类型,修改方法如下:

    public static void printCollection(Collection<? extends Person> list) {
        Iterator<? extends Person> it = list.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
  • 相关阅读:
    windows中dos命令指南
    HDU 2084 数塔 (dp)
    HDU 1176 免费馅饼 (dp)
    HDU 1004 Let the Balloon Rise (map)
    变态杀人狂 (数学)
    HDU 2717 Catch That Cow (深搜)
    HDU 1234 开门人和关门人 (模拟)
    HDU 1070 Milk (模拟)
    HDU 1175 连连看 (深搜+剪枝)
    HDU 1159 Common Subsequence (dp)
  • 原文地址:https://www.cnblogs.com/akiyama/p/10225175.html
Copyright © 2011-2022 走看看