zoukankan      html  css  js  c++  java
  • java-集合框架-泛型1

    package cn.burce.Genetic;
    
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashSet;
    import java.util.Iterator;
    
    import cn.burce.API3.Interger;
    
    /*
     * 1.5后出现安全机制 保证程序安全运行
     * 泛型:指明了集合中存储数据的类型<数据类型>
     * 不指明的情况下只能通过强转来实现数据的转换
     * 泛型的通配符?
     * 
     */
    public class GeneticLearn {
        public static void main(String[] args) {
            function();
            ArrayList<String> ARR = new ArrayList<String>();
            HashSet<Integer> ha = new HashSet<Integer>();
            ARR.add("笑笑");
            ARR.add("电风扇");
            ha.add(22);
            ha.add(33);
            function1(ARR);
            function1(ha);
            /*
             * 定义方法,可以同时迭代2个不同类型集合 参数:怎么实现,不能写ArrayList也不能写HashSet 参数:共同实现的接口
             */
        }
    
        private static void function() {
            Collection c = new ArrayList();
            c.add("我");
            c.add("我我我");
            c.add("额鹅鹅鹅我");
            c.add("凄凄切切我");
            // java.lang.Integer cannot be cast to java.lang.String
            // c.add(111);//下面的强转会出错
            Iterator it = c.iterator();
            while (it.hasNext())
            {
                String s = (String) it.next();
                System.out.println(s);
                System.out.println(s.length());
            }
        }
    
        private static void function1(Collection<?> coll) {
            Iterator<?> it1 = coll.iterator();
            while (it1.hasNext())
            {
                System.out.println(it1.next());
            }
        }
    }

  • 相关阅读:
    C primer plus 5 读书笔记2
    c primer plus 5 读书笔记1
    控制反转(IOC)模式
    软件设计原则
    springmvc跨域
    由阿里巴巴笔试题看java加载顺序
    spring各个包之间的依赖关系
    spring mvc 国际化
    git 笔记
    eclipse中maven项目部署到tomcat
  • 原文地址:https://www.cnblogs.com/BruceKing/p/13397394.html
Copyright © 2011-2022 走看看