JDK1.5新特性(六)Generics

概述

Generics – This long-awaited enhancement to the type system allows a type or method to operate on objects of various types while providing compile-time type safety. It adds compile-time type safety to the Collections Framework and eliminates the drudgery of casting.

泛型的出现主要是为了解决集合类元素的类型规范,即将集合类参数化,传入规定的元素类型参数,规范其元素的类型,这样就避免了很多类型转换上的异常,下面我们由浅入深,,慢慢来介绍

泛型的简单应用

虽然泛型的底层实现略有不爽,但是在表层的使用上还是很好理解的,至少,一些简单的使用就可以解决一大部分问题

下面我们来看一下

过去我们使用集合类来存储和获取对象时是这样做的

main(String[] args) { 2:// TODO Auto-generated method stub 3: 4://过去使用集合类的方法 5:List list = new ArrayList(); 6:list.add(1); 7:list.add(2); 8:list.add("1"); 9:Integer retVal = (Integer)list.remove(2); 10:System.out.println(retVal); 11: }那我想明天可以是我的来世。

JDK1.5新特性(六)Generics

相关文章:

你感兴趣的文章:

标签云: