我想让我的key和value 对应着显示,即

aa
2
bb
3
cc

 
 map.put("1",aa);
 map.put("2",bb);
 map.put("3",cc);
//怎么循环输出我想要的结果

解决方案 »

  1.   

    用数字做KEY的还不如直接一维数组就可以了,还用HASHMAP,-_-
      

  2.   

    HashMap map = new HashMap();
    map.put("1", "aa");
    map.put("2", "bb");
    map.put("3", "cc");

    for(Map.Entry entry:map.entrySet()){
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
    }---jdk1.5+
      

  3.   


    //jdk1.4   刚才不好意思 我的环境是1.4的  想1.5写起来简单点 就没有测试! Map map = new LinkedHashMap();
    map.put("1", "aa");
    map.put("2", "bb");
    map.put("3", "cc");

    Iterator it = map.entrySet().iterator();
    Map.Entry entry = null;
    while(it.hasNext()){
    entry = (Map.Entry)it.next();
    System.out.println(entry.getKey());
    System.out.println(entry.getValue());
    }这个应该满足你的要求了! 如果 你不要求顺序 你可以把LinkedHashMap 改成HashMap
      

  4.   

    caiming250 这位大哥,不好意思,就6分了,谢谢你了,