zoukankan      html  css  js  c++  java
  • 容器中使用泛型

    容器相关类都定义了泛型,我们在开发和工作中,在使用容器类时都要使用泛型。这样,在容器的存储数据、读取数据时都避免了大量的类型判断,非常便捷。

    【示例】泛型类的在集合中的使用

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class Test {
        public static void main(String[] args) {
            // 以下代码中List、Set、Map、Iterator都是与容器相关的接口;
            List<String> list = new ArrayList<String>();
            Set<Man> mans = new HashSet<Man>();
            Map<Integer, Man> maps = new HashMap<Integer, Man>();
            Iterator<Man> iterator = mans.iterator();
        }
    }

          通过阅读源码,我们发现Collection、List、Set、Map、Iterator接口都定义了泛型,如下图所示:

    图9-2容器的泛型定义.png

          因此,我们在使用这些接口及其实现类时,都要使用泛型。

    菜鸟雷区

          我们只是强烈建议使用泛型。事实上,不使用编译器也不会报错!

  • 相关阅读:
    yum install mysql.i686
    firefox无法浏览flash的解决方案
    vb.net如何打开指定文件
    XML文件操作的简单类
    window server 安装与卸载
    常用的sql语句
    with进行递归表
    常用js
    调用Google的自动翻译
    MySQl 总结知识
  • 原文地址:https://www.cnblogs.com/huaxiansheng/p/15317749.html
Copyright © 2011-2022 走看看