2015-06-20 30 views
-2

The phone key which I'm trying to get from the hashtablehashtable.get不工作 - java的

上圖爲我試圖從哈希表中檢索電話鍵。它返回null,因爲哈希表具有該鍵,如下圖所示。

This picture shows that Hashtable does contain the key I'm requesting in the above picture. But it's returning null. Why is so?

爲什麼是Hashtable返回null時,它的關鍵。請幫我從幾個小時卡住。

+2

可能是因爲這樣:http://stackoverflow.com/questions/6485186/if-key-in-hashtable-is-a-class-object-how-does-containskey-work – Surely

+2

問題中的郵政編碼爲文字,而不是圖像。 – chrylis

回答

3

您需要查看PhoneNum類的.equals()方法。哈希使用equals()來確定密鑰是否等於從get()的參數。請注意,如果您爲PhoneNum編寫equals(),則還需要編寫一個PROPER hashcode(),因此它不僅僅是寫作平等的問題。查找equals和hashcode,以便知道自己在做什麼。