找不到intValue方法 我制作了一个程序,用HashMap计算字数。这里是-: 导入java.util.*; 班级人数{ 公共静态void main(字符串参数[]){ 扫描仪s=新的扫描仪(System.in); 字符串in=s.nextLine(); HashMap hm=新的HashMap(); 字符串sh[]=in.split(“”); 对于(int i=0;i 1){ if(hm.get(key)==null){ hm.put(键,i); } 否则{ int value=新整数(hm.get(key).intValue()); 值++; hm.put(键、值); } } } 系统输出打印项次(hm); } }
但是在这个程序中,我得到了一个错误,在我使用jdk 1.6时,没有找到.intValue()符号。添加了自动装箱和取消装箱功能,所以我想这就是问题所在。我想计算计数,所以请给我解决方案。您应该编写此代码找不到intValue方法 我制作了一个程序,用HashMap计算字数。这里是-: 导入java.util.*; 班级人数{ 公共静态void main(字符串参数[]){ 扫描仪s=新的扫描仪(System.in); 字符串in=s.nextLine(); HashMap hm=新的HashMap(); 字符串sh[]=in.split(“”); 对于(int i=0;i 1){ if(hm.get(key)==null){ hm.put(键,i); } 否则{ int value=新整数(hm.get(key).intValue()); 值++; hm.put(键、值); } } } 系统输出打印项次(hm); } },java,Java,但是在这个程序中,我得到了一个错误,在我使用jdk 1.6时,没有找到.intValue()符号。添加了自动装箱和取消装箱功能,所以我想这就是问题所在。我想计算计数,所以请给我解决方案。您应该编写此代码 I have made a programme to count the number of words using HashMap.Here it is-: import java.util.*; class Count{ public static void main(Strin
I have made a programme to count the number of words using HashMap.Here it is-:
import java.util.*;
class Count{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
String in=s.nextLine();
HashMap hm=new HashMap();
String sh[]=in.split(" ");
for(int i=0;i<sh.length;i++){
String key=sh[i];
if(sh[i].length() > 1){
if(hm .get(key)==null){
hm.put(key,i);
}
else{
int value=new Integer(hm.get(key).intValue());
value++;
hm.put(key,value);
}
}
}
System.out.println(hm);
}
}
或者更好
int value=new Integer((Integer)hm.get(key)).intValue();
您应该编写此代码
I have made a programme to count the number of words using HashMap.Here it is-:
import java.util.*;
class Count{
public static void main(String args[]){
Scanner s=new Scanner(System.in);
String in=s.nextLine();
HashMap hm=new HashMap();
String sh[]=in.split(" ");
for(int i=0;i<sh.length;i++){
String key=sh[i];
if(sh[i].length() > 1){
if(hm .get(key)==null){
hm.put(key,i);
}
else{
int value=new Integer(hm.get(key).intValue());
value++;
hm.put(key,value);
}
}
}
System.out.println(hm);
}
}
或者更好
int value=new Integer((Integer)hm.get(key)).intValue();
参数化映射:使用
HashMap
而不是HashMap
:
int value = (Integer)hm.get(key);
通常,您不会像HashMap
那样将变量声明为具体实现,而是使用通用Map
接口:
int value= hm.get(key);
value++;
hm.put(key,value);
Map hm=newhashmap();
参数化映射:使用HashMap
而不是HashMap
:
int value = (Integer)hm.get(key);
通常,您不会像HashMap
那样将变量声明为具体实现,而是使用通用Map
接口:
int value= hm.get(key);
value++;
hm.put(key,value);
Map hm=newhashmap();
因此您有一个HashMap
,它不指定存储在其中的对象类型。要指定存储在其中的内容,需要使用泛型。
将hashmap定义为
hashmap hm=newhashmap()代码>
否则,存储在其中的所有内容都将被视为仅对象
,而对象
没有intValue方法。因此,该错误由编译器抛出。因此您有一个HashMap
,它没有指定存储在其中的对象类型。
要指定存储在其中的内容,需要使用泛型。
将hashmap定义为hashmap hm=newhashmap()代码>
否则,存储在其中的所有内容都将被视为仅对象
,而对象
没有intValue方法。所以这个错误是由编译器抛出的。不,单词应该是你的键,单词的数量应该是你的值…在那里响应,但你不需要创建一个新的整数…是的,如果你使用JDK6,你可以使用泛型,也就是新的HashMap():)不,单词应该是你的键,字数应该是你的值…在那里有响应,但你不需要创建一个新的整数…是的,如果你使用JDK6,你可以使用泛型,也就是新的HashMap():)它并不是真的缺少a),而是放错了位置。:)您的代码无法编译hm.get(key)
是一个对象,Integer
没有接受对象的构造函数。它并没有真正缺少a),而是放错了位置。:)您的代码无法编译hm.get(key)
是一个对象,Integer
没有接受对象的构造函数。