日期:2014-05-20  浏览次数:20840 次

对Integer的理解
import   java.util.*;
public   class   Freq{

public   static   void   main(String[]   args)
{
String[]   words={ "if ", "it ", "is ", "to ", "be ", "it ", "is ", "up ", "to ", "me ", "to ", "delegate "};
Integer   freq;

Map   m=new   TreeMap();

for(int   i=0;i <words.length;i++){

freq=(Integer)   m.get(words[i]);

if(freq==null){

freq=new   Integer(1);
}
else   {
freq=new   Integer(freq.intValue()+1);
}

m.put(words[i],freq);}
System.out.println(m.size()+ "distinct   words   detected: ");
System.out.println(m);}


}
我知道Integer是int   的封专类,但具体的为什么有时候只用它,而不用int   就好比上面这个例子,请问integer的作用是什么,能用int   替代吗?为什么

------解决方案--------------------
int不是Object

Integer是

当你要把这些数放在容器里面的时候

就要用上Integer了


------解决方案--------------------
简单的理解,容器就像数组,是一种数据结构
------解决方案--------------------
Integer是一个对象。是一个封装类。
而int是类型。是java中的一种数据类型。
Integer是很好的封装类
------解决方案--------------------
容器里里只能放对象,不能存放基本类型
这种情况下,不得不把基本类型转为对应的包装类型了
------解决方案--------------------
楼上正解
现在的包装类都可以自动装箱解相
很方便
------解决方案--------------------
回复人:wlsx2003() ( 一级(初级)) 信誉:100 2007-2-18 16:50:00 得分:0
?

简单的理解,容器就像数组,是一种数据结构


象散列表啊 堆栈啊 之类的都是容器