我的任务是在 HashMap 中实现联系人列表。除了下面代码中的问题之外,一切都很顺利。 HashMap 方法 put(K key, V value) 不接受定义的参数 String、List。
public class ContactList(){ // this is the class
private HashMap<String, List<String>> map; // private HashMap field
public void update(String name, List<String> number){ //method I'm having trouble with
this.map.remove(name);
this.map.put(name, number) // HashMap method, main problem.
}
}
错误是:
The method put(String, List<String>) is undefined for the type ContactList
如何纠正这个问题?
最佳答案
您正在尝试在构造函数中调用并定义类变量。而且您在同一个构造函数中编写方法更新,这也是错误的。
试试这个。
public class ContactList{
private Map<String,List<String>> map;
public ContactList(){
map = new HashMap<String,List<String>>();
String contactName = "Shyama Bhattacharya";
List<String> constactAddress = new ArrayList<String>();
contactAddress.add("Parker Colony");
contactAddress.add("Banglaru");
update(contactName,contactAddress);
}
public void update(String contactName,List<String> contactAddress){
map.put(contactName,contactAddress);
}
}
这绝对应该有效!!
关于java - Hashmap 方法不接受定义的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9947363/