在<;类型的java中为map使用getValue方法;字符串,整数>;
执行以下部分时,在<;类型的java中为map使用getValue方法;字符串,整数>;,java,maps,typecasting-operator,getvalue,Java,Maps,Typecasting Operator,Getvalue,执行以下部分时,getValue()方法中出现错误。我尝试在getValue()中将s作为参数传递。在那里也不起作用 //Mymap<String,Integer>() while(in.hasNext()){ //in is a scanner object String s = in.next(); // Write code here //s is a string to be searched if (Mymap.cont
getValue()
方法中出现错误。我尝试在getValue()
中将s作为参数传递。在那里也不起作用
//Mymap<String,Integer>()
while(in.hasNext()){
//in is a scanner object
String s = in.next();
// Write code here
//s is a string to be searched
if (Mymap.containsKey(s)) {
//the value corresponding to s is to be retrieved
Integer i= (Integer)Mymap.getValue();
System.out.println(i);
System.out.println(s+"="+Mymap.get(s));
} else {
System.out.println("Not found");
}
}
//Mymap()
while(在.hasNext()中){
//中是扫描仪对象
字符串s=in.next();
//在这里写代码
//s是要搜索的字符串
如果(Mymap.containsKey){
//要检索与s对应的值
整数i=(整数)Mymap.getValue();
系统输出打印LN(i);
System.out.println(s+“=”+Mymap.get(s));
}否则{
System.out.println(“未找到”);
}
}
好的,正如其他人指出的,方法getValue()
不存在,因此编译错误
HashMap类的所有方法
如果需要与键s
对应的整数值并将其存储在i
中,则应调用Mymap.get(s)
。。。就像你在排队时做的那样:
System.out.println(s+"="+Mymap.get(s));
什么是
Mymap
getValue
在接口Map
中不存在,它只存在于Map中。Entry
在EntrySet@RouliboygetValue上迭代在Map接口上没有意义,它不存在。如果需要,可以迭代EntrySet。如果要获取特定键的值,请使用Map.get
。但我不明白你试图用getValue做什么