Hashmap方法不接受已定义的参数

Oca*_*shu 0 java hashmap

我的任务是在HashMap中实现联系人列表.除了以下代码中的问题外,一切顺利.HashMap方法put(K键,V值)不接受已定义的参数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.
  }
}
Run Code Online (Sandbox Code Playgroud)

错误是:

 The method put(String, List<String>) is undefined for the type ContactList
Run Code Online (Sandbox Code Playgroud)

我该如何纠正?

PVR*_*PVR 8

您正尝试在Constructor中调用和定义类变量.而你在相同的构造函数中写入方法更新也是错误的.

试试吧.

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);
   }

}
Run Code Online (Sandbox Code Playgroud)

这绝对有用!!

  • 我没有从构造函数调用重写方法.请详细说明你的陈述. (2认同)