离弦之Ray

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  55 Posts :: 0 Stories :: 33 Comments :: 0 Trackbacks
一直疑惑 ArrayList<int> 怎么搞,但懒得去查,今天网上搜到了:

The generics facility in Java can be used only for
object (reference) types.  It cannot be used primitive
types like 'int' or 'float'.

So, if you wanted an ArrayList that contained only
integer data, then you'd need to use the object type
which corresponds to int: java.lang.Integer.

   ArrayList<Integer> numbers;
   numbers = new ArrayList<Integer>(40);

Fortunately, the new Autobox/unbox features of Java can
make this work just like an ArrayList of int.

   numbers.add(27);

The value 27 will be automatically boxed as an Integer
object, then added to the ArrayList<Integer>.

For more information, consult
http://java.sun.com/developer/technicalArticles/J2SE/generics/index.html
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf

posted on 2007-12-23 15:22 离弦之ray的技术天空 阅读(926) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: