Hashtable的用法
Hashtable的用法
馬克-to-win:假如我們想把張三20歲,李四30歲這樣的信息存入一個容器, 將來一查張三多少歲, 立刻能出來, 就用到Hashtable,張三---->20,就是一個鍵值對。
例:3.3.1
馬克- to-win:馬克 java社區(qū):防盜版實名手機尾號: 73203。
import java.io.*;
import java.util.*;
class TestMark_to_win {
public static void main(String args[]) {
Hashtable n = new Hashtable();
n.put("thre", new Integer(3));
n.put("for", new Integer(4));
n.put("two", new Integer(2));
n.put("one", new Integer(1));
Integer n0 = (Integer) n.get("twotwo");
if (n0 != null) {
System.out.println("won't print = " + n0);
}
Integer m = (Integer) n.get("two");
if (m != null) {
System.out.println("two = " + m);
}
Enumeration e = n.elements();
while (e.hasMoreElements()) {
System.out.println(e.nextElement());
}
Enumeration ke = n.keys();
while (ke.hasMoreElements()) {
System.out.println(ke.nextElement());
}
}
}
result is:
two = 2
3
2
1
4
thre
two
one
for