如何在HashMap
中搜索密鑰?在這個程序中,當用戶輸入一個密鑰時,代碼應該安排在hashmap中搜索對應的值並打印出來。在HashMap中搜索給定密鑰的值
請告訴我爲什麼它不起作用。
import java.util.HashMap;
import java.util.; import java.lang.;
public class Hashmapdemo
{
public static void main(String args[])
{
String value;
HashMap hashMap = new HashMap();
hashMap.put(new Integer(1),"January");
hashMap.put(new Integer(2) ,"February");
hashMap.put(new Integer(3) ,"March");
hashMap.put(new Integer(4) ,"April");
hashMap.put(new Integer(5) ,"May");
hashMap.put(new Integer(6) ,"June");
hashMap.put(new Integer(7) ,"July");
hashMap.put(new Integer(8),"August");
hashMap.put(new Integer(9) ,"September");
hashMap.put(new Integer(10),"October");
hashMap.put(new Integer(11),"November");
hashMap.put(new Integer(12),"December");
Scanner scan = new Scanner(System.in);
System.out.println("Enter an integer :");
int x = scan.nextInt();
value = hashMap.get("x");
System.out.println("Value is:" + value);
}
}
製作務必通過點擊旁邊的「打勾」來接受回答您的問題的答案。 – 2010-06-16 07:56:29