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

    泛型: 指明了集合中存储数据的类型  <数据类型>

    public class GenericDemo {
          public static void main(String[] args) {
            function();
          }
          
          public static void function(){
            Collection<String> coll = new ArrayList<String>();
            coll.add("abc");
            coll.add("rtyg");
            coll.add("43rt5yhju");
        
            
            Iterator<String> it = coll.iterator();
            while(it.hasNext()){
              String s = it.next();
              System.out.println(s.length());
            }
          }
        }

    Java中的伪泛型:
    泛型只在编译时存在,编译后就被擦除,在编译之前我们就可以限制集合的类型,起到作用
    例如:ArrayList<String> al=new ArrayList<String>();
    编译后:ArrayList al=new ArrayList();

    a:定义格式:
    修饰符 class 类名<代表泛型的变量> { }

    例如,API中的ArrayList集合:
    class ArrayList<E>{
    public boolean add(E e){ }
    public E get(int index){ }
    }

    b:使用格式:
    创建对象时,确定泛型的类型

    例如,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){  }
          }

    泛型的方法

    格式:修饰符 <代表泛型的变量> 返回值类型 方法名(参数){  }

    使用格式:调用方法时,确定泛型的类型
        例如:
              ArrayList<String> list = new ArrayList<String>();
              String[] arr = new String[100];
              String[] result = list.toArray(arr);
           此时,变量T的值就是String类型。变量T,可以与定义集合的泛型不同
           public <String> String[] toArray(String[] a){  } 
        
          例如:
              ArrayList<String> list = new ArrayList<String>();
              Integer[] arr = new Integer[100];
              Integer [] result = list.toArray(arr);
          
          此时,变量T的值就是Integer类型。变量T,可以与定义集合的泛型不同
          public <Integer> Integer[] toArray(Integer[] a){  } 

    泛型的接口:

    带有泛型的接口

    public interface List <E>{
              abstract boolean add(E e);
          }
          

    实现类,先实现接口,不理会泛型

    public class ArrayList<E> implements List<E>{
      }

    :泛型的好处

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

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

    泛型的通配符

    public class GenericDemo {
        public static void main(String[] args) {
          ArrayList<String> array = new ArrayList<String>();
          
          HashSet<Integer> set = new HashSet<Integer>();
          
          array.add("123");
          array.add("456");
          
          set.add(789);
          set.add(890);
          
          iterator(array);
          iterator(set);
        }
        /*
         *  定义方法,可以同时迭代2个集合
         *  参数: 怎么实现 , 不能写ArrayList,也不能写HashSet
         *  参数: 或者共同实现的接口
         *  泛型的通配,匹配所有的数据类型  ?
         */
        public static void iterator(Collection<?> coll){
          Iterator<?> it = coll.iterator();
          while(it.hasNext()){
            //it.next()获取的对象,什么类型
            System.out.println(it.next());
          }
        }
       }
  • 相关阅读:
    python-进程池实例
    python-进程通过队列模拟数据的下载
    python-多进程模板
    python-多线程同步中创建互斥锁解决资源竞争的问题
    CentOS6.5配置网络
    解决CentOS系统Yum出现"Cannot find a valid baseurl for repo"问题
    CentOS 6.5安装图形界面
    Centos安装git
    Web前端优化,提高加载速度
    谁说写代码的不懂生活
  • 原文地址:https://www.cnblogs.com/zimo-bwl1029-s/p/9551351.html
Copyright © 2011-2022 走看看