[英]why can't i do the hashmap.put()
Whenever i try to run my error is this "hmap.put(id, b0);"每当我尝试运行我的错误是这个“hmap.put(id,b0);” am i doing it correctly?
我做得对吗? im trying to do a user input and insert it in a hashmap.
我正在尝试进行用户输入并将其插入 hashmap。 it says: no suitable method found for put(Integer,Student) method Map.put(Integer,String) is not applicable (argument mismatch; Student cannot be converted to String) method AbstractMap.put(Integer,String) is not applicable (argument mismatch; Student cannot be converted to String) method HashMap.put(Integer,String) is not applicable
它说:没有找到适合 put(Integer,Student) 方法 Map.put(Integer,String) 的方法不适用(参数不匹配;学生不能转换为字符串)方法 AbstractMap.put(Integer,String) 不适用(参数不匹配;学生无法转换为字符串)方法 HashMap.put(Integer,String) 不适用
package javaapplication30;
import java.util.*;
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
class Student {
int id;
String sn, cor;
public Student(int id, String sn, String cor) {
this.id = id;
this.sn = sn;
this.cor = cor;
}
}
public class JavaApplication30 {
public static void main(String[] args) {
HashMap < Integer, String > hmap = new HashMap < Integer, String > ();
Scanner sc = new Scanner(System.in);
for (int i = 0; i < 2; i++) {
System.out.print("id: ");
Integer id = sc.nextInt();
System.out.print("name: ");
String sn = sc.next();
System.out.print("course: ");
String cor = sc.next();
Student b0 = new Student(id, sn, cor);
hmap.put(id, b0);
}
for (Map.Entry m: hmap.entrySet()) {
System.out.println(m.getKey() + " " + m.getValue());
}
}
}
You have declared the map to be a HashMap<Integer, String>
.您已将 map 声明为
HashMap<Integer, String>
。 In other words, the key type is Integer
and the value type is String
.换句话说,键类型是
Integer
,值类型是String
。
But then you do this:但是你这样做:
Student b0 = new Student(id, sn, cor);
hmap.put(id, b0);
That is attempting to add a map entry where the value is a Student
.这是试图添加一个值为
Student
的 map 条目。
Student
is not a subclass of String
so that is not legal. Student
不是String
的子类,因此这是不合法的。
Here is what the error message says, and how to interpret it:以下是错误消息的内容,以及如何解释它:
no suitable method found for put(Integer,Student)
That corresponds to this call put(id, b0)
.这对应于这个调用
put(id, b0)
。 Observe that id
is declared to be an Integer
and b0
is a Student
观察
id
被声明为Integer
并且b0
是Student
method Map.put(Integer,String) is not applicable
The compiler has found a put
method in the Map
interface whose signature is put(Integer,String)
.编译器在
Map
接口中找到了一个put
方法,其签名为put(Integer,String)
。 It has the correct name and the correct number of parameters.它具有正确的名称和正确数量的参数。 But...
但...
(argument mismatch; Student cannot be converted to String)
The compiler has tried to find a legal way to use that put
method.编译器试图找到一种合法的方式来使用该
put
方法。 The first argument is compatible, but there is no conversion that will convert a Student
(which is what the argument is) to a String
(which is what the method requires).第一个参数是兼容的,但是没有将
Student
(这是参数)转换为String
(这是方法所需要的)的转换。
The solution is to change the declaration of hmap
:解决方案是更改
hmap
的声明:
HashMap<Integer, Student> hmap = new HashMap<>();
(The <>
tells the compiler to infer (ie work out) the generic type parameters from the context.) (
<>
告诉编译器从上下文中推断(即计算出)泛型类型参数。)
You declared your Hasmap as: new HashMap<Integer, String>();
您将 Hasmap 声明为:
new HashMap<Integer, String>();
Then your HashMap expects Integer as key and String as value.然后您的 HashMap 期望 Integer 作为键和字符串作为值。 Your object b0 is not a String, it's a Student object.
您的 object b0 不是字符串,而是学生 object。
Then you should change your HashMap as new HashMap<Integer, Student>();
然后你应该将你的 HashMap 更改为
new HashMap<Integer, Student>();
( or you can call toString() function on b0, it depends on what do you want to do) (或者你可以在 b0 上调用 toString() function,这取决于你想做什么)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.