| | 1 | {{{ |
| | 2 | #!html |
| | 3 | <div style="text-align: center; color:#151B8D"><big |
| | 4 | style="font-weight: bold;"><big><big> |
| | 5 | Java Programing - Map 用法 |
| | 6 | </big></big></big></div> |
| | 7 | <div style="text-align: center; color:#7E2217"><big |
| | 8 | style="font-weight: bold;"><big> |
| | 9 | 使用Map介面之實做類別 Hashtable |
| | 10 | </big></big></div> |
| | 11 | }}} |
| | 12 | [[PageOutline]] |
| | 13 | |
| | 14 | |
| | 15 | 要達成 Map 的 key value 對應程式,也可以用hashmap,不過hashmap的功能與選項較多,因此挑較單純的hashtable來使用 |
| | 16 | |
| | 17 | * 用法: |
| | 18 | |
| | 19 | {{{ |
| | 20 | 要加入元素用 map.put(key,value); |
| | 21 | 要取出元素用 map.get(key); |
| | 22 | }}} |
| | 23 | |
| | 24 | * Example : |
| | 25 | |
| | 26 | {{{ |
| | 27 | #!java |
| | 28 | String[] key = { "this is a cat", "this is a dog", "this is a fish", }; |
| | 29 | int[] value = { 1, 2, 3 }; |
| | 30 | Hashtable<String, Integer> table = new Hashtable<String, Integer>(); |
| | 31 | for (int i = 0;i < key.length ; i++){ |
| | 32 | table.put(key[i], value[i]); |
| | 33 | } |
| | 34 | System.out.println("value = " + table.get("this is a ")); |
| | 35 | }}} |