📄 chapter21n2.java
字号:
/** * * demonstration of Hashtable * * Written by: Roger Garside * * First Written: 5/Feb/97 * Last Rewritten: 5/Feb/97 * */import java.io.* ;import java.util.* ;class PersonDates { private int birth, death ; /** * * constructor * */ public PersonDates (int b, int d) { birth = b ; death = d ; } // end of constructor method public int getBirthDate() { return birth ; } // end of method getBirthDate public int getDeathDate() { return death ; } // end of method getDeathDate } // end of class PersonDatespublic class Chapter21n2 { public static void main(String[] args) throws Exception { Hashtable t = new Hashtable() ; t.put("shakespeare", new PersonDates(1564, 1616)) ; t.put("austen", new PersonDates(1775, 1817)) ; t.put("dickens", new PersonDates(1812, 1870)) ; t.put("trollope", new PersonDates(1815, 1882)) ; t.put("bronte", new PersonDates(1818, 1848)) ; t.put("eliot", new PersonDates(1819, 1880)) ; PersonDates result = (PersonDates) t.put("bronte", new PersonDates(1816, 1855)) ; if (result != null) System.err.println("duplicate information") ; System.err.println() ; System.err.print("type author's name ") ; System.err.flush() ; BufferedReader din = new BufferedReader(new InputStreamReader(System.in)) ; String name = din.readLine().trim() ; PersonDates p = (PersonDates) t.get(name) ; if (p == null) System.err.println("I have no information about " + name) ; else System.err.println(name + " was born in " + p.getBirthDate() + " and died in " + p.getDeathDate()) ; System.err.println() ; Enumeration hashElements = t.elements() ; Enumeration hashKeys = t.keys() ; while (hashKeys.hasMoreElements()) { System.err.print(hashKeys.nextElement()) ; PersonDates p1 = (PersonDates) hashElements.nextElement() ; System.err.println(" was born in " + p1.getBirthDate() + " and died in " + p1.getDeathDate()) ; } } // end of method main } // end of class Chapter21n2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -