这个面试题应该怎么做呢?我查到的都是.NET做的。请哪位高手写一个用java遍历hashtable的程序,谢谢!

解决方案 »

  1.   


    private static void test() {
    Hashtable<String,String> table = new Hashtable<String,String>();
    table.put("1", "hello");
    table.put("2", "world!");
    Iterator i = table.entrySet().iterator();
    while(i.hasNext()){
    Entry entry = (Entry) i.next();
    System.out.println(entry.getValue());
    }

      

  2.   

    public static void printall(Hashtable ht){
        Enumeration en = ht.keys();
           while(en.hasMoreElements()){
              Object ele = en.nextElement();
              System.out.println(ele);
           }
        }
    }大概就是这样的
      

  3.   

    请问anqini
    “1”和“2”怎么和“hello”"world!"一起输出呢?
      

  4.   

            Hashtable ht = new Hashtable();
            ht.put("zhangsan", new Integer(1));
            ht.put("lisi", new Integer(2));
            ht.put("wangwu", new Integer(3));        Enumeration e = ht.keys();
            while (e.hasMoreElements()) // 迭代
            {
                String key = (String) e.nextElement();
                System.out.println(key.toString() + "=" + ht.get(key));
            }
      

  5.   

    偶也为楼主写了一个,程序具体解释请看注释.
    import java.util.*;
    public class HashtableTest {
    public static void main(String[] args) {
    HashtableTest ht = new HashtableTest();
    ht.getHashtable();
    }
    public void getHashtable() {
    Hashtable numbers = new Hashtable();
    numbers.put("卓彬", "湖南第一师范");
    // 用put()方法向Hashtable对象中放数据,前面是键,后面是值
    numbers.put("刘少", "中南大学");
    Enumeration e = numbers.keys();
    while (e.hasMoreElements()) {
    String it = (String) e.nextElement();
    // 通过nextElement返回指示器指向的那个对象,并将指示器指向下一个对象
    System.out.println(it + "毕业于" + numbers.get(it));
    //打印出键值对!
    }
    }
    }