RTpublic class MapTest2 {
public Map<String, Integer> putkey(String string, Integer integer) {
Map<String, Integer> tempMap = new HashMap<String, Integer>();
tempMap.put("a", 1);
tempMap.put("b", 2);
tempMap.put("c", 3);
return tempMap; } public static void main(String[] args) {
Map map = new HashMap();
 MapTest2 mt = new MapTest2();

Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println(" key " + key + " value " + value);
}
}
}

解决方案 »

  1.   

    这段代码,我就是想把键值对读出来,但是main函数怎么写了?
      

  2.   

    Map map = putkey("",0);
      

  3.   

    是读出来,数据都写进去了
    是new HashMap(); 
    还是 new MapTest2(); 
      

  4.   


    putkey方法 前加个static
      

  5.   

    public class MapTest2 {
    public  static Map<String, Integer> putkey(String string, Integer integer) {
    Map<String, Integer> tempMap = new HashMap<String, Integer>();
    tempMap.put("a", 1);
    tempMap.put("b", 2);
    tempMap.put("c", 3);
    return tempMap; } public static void main(String[] args) {
    Map map =  putkey("",0);
    Iterator it = map.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry entry = (Entry) it.next();
    Object key = entry.getKey();
    Object value = entry.getValue();
    System.out.println(" key " + key + " value " + value);
    }
    }
    }
      

  6.   

     key a value 1
     key c value 3
     key b value 2
      

  7.   

    能否解释一下代码啊?我不会Map
      

  8.   

    终于看明白了!MapTest2 mt = new MapTest2(); 
    Map map = mt.putkey(null, null);
    Iterator it = map.entrySet().iterator(); 
    ...
      

  9.   

    Map map =  putkey("",0);
    为啥也可以了?
      

  10.   

    我知道要new 一个实例,但是这个写法“Map map =  putkey("",0);”不知道
    谁帮忙解释下