如何在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