这是我的代码...
public class HashTest {
public static void main(String[] args) {
HashTest ht = new HashTest();
ht.test(?,"abc");
}
public void test(HashMap<String, String> hm, String country) {
Scanner sc = new Scanner(System.in);
System.out.println("No. of Input : ");
int n = sc.nextInt();
System.out.println("Enter Values : ");
String capital = "";
int city;
for (int i = 0; i < n ; i++) {
country = sc.next();
capital = sc.next();
hm.put(country, capital);
if (hm.containsValue(country)) {
System.out.println("Result : " +hm.get(capital));
}
}
调用测试方法时,现在在main方法内部,那么什么是实际参数代替?
?
答案 0 :(得分:0)
如果您只是为了测试而执行main,则可以执行以下操作:
public static void main(String[] args) {
HashTest ht = new HashTest();
HashMap hm=new HashMap<String, String>();
ht.test(hm,"abc");
}
如果有人从“外部”使用此类。他们会这么做
//some user code
HashTest ht= new HashTest();
HashMap hm=new HashMap<String, String>();
ht.test(hm,"abc");
//more user code
实际上是一样的。
还有更多创建和初始化HashMap的方法。选中Different Ways of Creating HashMaps