zoukankan      html  css  js  c++  java
  • 泛型和增强型for循环。摘记

    没有泛形的Collections 库的代码

    ArrayList list = new ArrayList();
    list.add(0, new Integer(42));
     
    int total = ((Integer)list.get(0)).intValue();

    带有范型化Collections 库的同一个例子可编写为:

    ArrayList<Integer> list =  new ArrayList<Integer>();
    list.add(0, new Integer(42));
    int total = list.get(0).intValue();
    范型化 API 的用户必须使用 <> 符号简单地声明在编译类型中使用的类型

    增强的 for 循环

    Collections API 经常使用 Iterator 类。Iterator 类提供在 Collection 中顺序导航的机制。

    当像下面一样只是在 Collection 中遍历时,新的增强的 for 循环可取代 iterator。

    编译器生成必要的循环代码,因为利用范型,所以不需要额外的类型转换。

    原来

    ArrayList<Integer> list = new ArrayList<Integer>();
    for (Iterator i = list.iterator(); i.hasNext();) {
    Integer value=(Integer)i.next();
    }

    现在

    ArrayList<Integer> list = new ArrayList<Integer>();
    for (Integer i : list) { ... }


  • 相关阅读:
    [转载]C#.NET中Dns类的常用方法及说明
    [转载]如何辨别真假百度蜘蛛
    Lottie的json动画
    iOT
    iOS字体大小
    针对Xcode 9 + iOS11 的修改,及iPhone X的适配
    shell脚本之 给PNG图片添加后缀@3x
    正则表达式
    CSS
    XcodeProj,使用Ruby更改工程文件
  • 原文地址:https://www.cnblogs.com/kanego/p/1966898.html
Copyright © 2011-2022 走看看