wiki:waue/2009/1022
Java Programing - Map 用法
使用Map介面之實做類別 Hashtable

要達成 Map 的 key value 對應程式,也可以用hashmap,不過hashmap的功能與選項較多,因此挑較單純的hashtable來使用

  • 用法:
要加入元素用 map.put(key,value);
要取出元素用 map.get(key);
  • Example :
    String[] key = { "this is a cat", "this is a dog", "this is a fish", };
    int[] value = { 1, 2, 3 };
    Hashtable<String, Integer> table = new Hashtable<String, Integer>();
    for (int i = 0;i < key.length ; i++){
      table.put(key[i], value[i]);
    }
    System.out.println("value = " + table.get("this is a "));
Last modified 14 years ago Last modified on Oct 30, 2009, 11:32:49 PM