由于集合类(Collection)里面只能保存从Object当中继承而来的对象,所以对于基本类型(Primitive Type)的变量,如int,float等,在添加到Collection当中时,需要使用他们对应的封装类Integer,Float等,如:
ArrayList
list.add(0, new Integer(42));
int total = (list.get(0)).intValue();
在5.0当中,通过使用Autoboxing和Auto-Unboxing我们就不再需要显式的使用这些封装类了。
ArrayList
list.add(0, 42);
int total = list.get(0);
十一 20
十一 20
J2SE 5.0新功能-Generic Types
技术相关 27 views, No Comments
在5.0以前,当我们需要从集合类当中提取出某个元素时,需要显式的对他进行下塑造型,如
ArrayList list = new ArrayList();
list1.add(0, new Integer(42));
int total = ((Integer)list1.get(0)).intValue();
在5.0当中,通过使用Generic Types,即将集合类(Collection)的声明写成Collection
ArrayList
list.add(0, new Integer(42));
int total = list.get(0).intValue();
近期评论